0

I'm trying to store classes in a 2d array, but with empty fields, or holes, i.e. fields that simply have no content. Since the class itself is quite big, I decided to fill the vector with pointers to the class, so empty fields could be simply written as NULL. I instantly hit a Wall. If I try to initialize the first value of the vector like this:

myVector[0][0]* = classToBeCopied;

I get the following errors: "Syntax Error" "expected primary-expression before '=' token"

I do have a working copy-constructor and operator= for the class. (I am using Eclipse Luna) Thank you in advance!

BigBadWolf
  • 603
  • 2
  • 8
  • 17
  • 5
    what's that `*`? Did you mean `myVector[0][0] = &classToBeCopied;`? – Antonio Apr 16 '15 at 19:57
  • 1
    If you're looking to store only copies, why not declare it as `std::vector>` to make copy the default? Seems counter-productive to store pointers. – tadman Apr 16 '15 at 20:00
  • @Antonio I think OP is trying to dereference the pointer stored the the nested `vector` to trigger the copy assignment operator. – James Adkison Apr 16 '15 at 20:01
  • @Antonio ^James Adkins is correct. For various reasons it could get messy using references. – BigBadWolf Apr 16 '15 at 20:04
  • @BigBadWolf Placed there, `&` does not mean reference, but "address of". You can find a good explanation [here](http://stackoverflow.com/questions/6877052/use-of-the-operator-in-c-function-signatures) – Antonio Apr 17 '15 at 07:00
  • Oh, thank you! The usage of references in C++ has always been confusing for me, always thinking of it as addresses (To me, a reference was the same thing as an "address of"). But, if I assign it by address, doesn't that turn bad when `classToBeCopied`s lifetime ends? – BigBadWolf Apr 17 '15 at 08:10

2 Answers2

3

Try:

*myVector[0][0] = classToBeCopied;
user2023370
  • 10,488
  • 6
  • 50
  • 83
  • Thank you! That worked. Does the asterisk positioning make a difference in C++? I'm fresh in from C. – BigBadWolf Apr 16 '15 at 20:01
  • 2
    a* = b; will try to parse as a *= b; in C too, so there is no difference in dereferencing syntax – Creris Apr 16 '15 at 20:33
  • I guess you can do something a thousand times in a row, and still get it wrong. I felt like the whitespace made it clear that it's not a *=. – BigBadWolf Apr 16 '15 at 20:42
  • @BigBadWolf I'm wondering how this solution can work for you. You said you _try to initialize the first value of the vector_ and you store pointers in your `vector` so I assume they are initialized with `0`. In this case `*myVector[0][0]` means dereferencing a null-pointer which should lead to a segmentation fault... – tomse Apr 16 '15 at 20:50
  • Jesus, I haven't even considered that yet. the [0][0] instance was created by push_back(class*), which may explain why I didn't get a segfault. I guess I will have to use `new` sooner or later. – BigBadWolf Apr 16 '15 at 21:07
2

myVector[0][0]* = classToBeCopied;

If the result of myVector[0][0] is a type class_ptr*. Then you could do the following.

class_ptr* p = myVector[0][0];
*p; // Dereference

Therefore the following should be sufficient

*myVector[0][0]
James Adkison
  • 9,412
  • 2
  • 29
  • 43