0

I have problem with pointers. My classes:

Zbior{
   Czasteczka* tablicaCzasteczek;  //it will be a pointer to dynamic array
   Zbior();
}
Czasteczka{
   Czasteczka();
   Czasteczka(int x, int y);
}

Constructor of Zbior:

Zbior::Zbior()
{
   this->tablicaCzasteczek = new Czasteczka[n];
   for( int i=0 ; i<n ; i++ )
   {
      this->tablicaCzasteczek[i] = NULL;   <-- here is 1st error
   }
   this->tablicaCzasteczek[0] = new Czasteczka(X, Y);   <-- 2nd error
   this->tablicaCzasteczek[1] = new Czasteczka(X, Y+1); <-- same error as above
}

Above code has to create dynamic array and add first and second object to this array.

Errors:

1.) Error 4 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

2.) Error 6 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Czasteczka *' (or there is no acceptable conversion) Thank for all help! :)

8
  • 2
    Czasteczka is not a pointer. What is assigning null or the result of new to one supposed to do? Commented May 4, 2014 at 17:34
  • Can you assign NULL to an instance of Czasteczka?? Commented May 4, 2014 at 17:35
  • 1
    It looks like you're familiar with Java. If so, there are many things you need to unlearn. Commented May 4, 2014 at 17:39
  • 2
    Unless you are doing this for "educational" purposes, save yourself a lot of trouble and use std::vector<Czasteczka> tablicaCzasteczek;. Commented May 4, 2014 at 17:42
  • 1
    '... that all of pointer are empty' There are no pointers! You already have n default constructed Czasteczka! Commented May 4, 2014 at 17:43

1 Answer 1

2

Czasteczka *tablicaCzasteczek is a pointer to an instance of Czasteczka. It can also be holding a pointer to the first element of a decayed array of Czasteczka. This is the crucial part to understand:

tablicaCzasteczek[0] = NULL; is equivalent to *tablicaCzasteczek = NULL. That means you're trying to assign NULL to an instance of Czasteczka.

If you really want to make your code work, declare tablicaCzasteczek as

Czasteczka **tablicaCzasteczek;

and later on

this->tablicaCzasteczek = new Czasteczka*[n];

But that is calling for trouble if you don't know what you're doing (I don't see any destructors in your example).

As others have suggested, you'll be having much less headaches if you use std::vector. You'd be much better off with something like this:

class Czasteczka
{
public:
    Czasteczka(int x, int y);
    Czasteczka(const Czasteczka &other);
};

std::vector <Czasteczka> zbiorCzasteczek;

And then if you're on C++11:

zbiorCzasteczek.emplace_back(someX, someY);

Or if you're not:

zbiorCzasteczek.push_back(Czasteczka(someX, someY));

You can then access you're elements just as you would with an array (zbiorCzasteczek[0]). In this case, you won't have to worry about memory management, as std::vector will take care of that for you.

Of course you can put the vector as your class' member as well. I left out your Zbior class, as in your example it didn't do anything in particular.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.