views:

155

answers:

4

New to C++ and its giving me a hissy fit.

The actual code is on a different computer and I can't copy it, so I'll do the best I can with pseudo code.

My endgame is to have a vector [of pointers] that can be accessed by several different objects. The way I attempted to go about this is have the original class that contains the vector to have a method something like:

std::vector<myObject*>& myClass::getVectorPointer(){
 return &vectorPointer;
}

So then my various classes end up having a line of code like this:

std::vector<myObject*>* myStuff = myClassObject.getVectorPointer();

Then I want to start throwing objects into the vector. So first I instantiate an object:

MyObject* tempObject = new MyObject("grrr");

And then I want to put it in the vector so I did this:

myStuff->push_back(tempObject);

This compiles just fine, but when I run it chokes. Going through the debugger I get:

402 wrong new expression in [some_]allocator::construct

I have no clue what this means. I tried putting :: in front of my new like something in the proposed solution said, but that didn't do diddly.

So, to review, pretend it was something like this, but with correct syntax and logic:

Class A.h
#include <vector>
#include "myObject.h"

ClassA{
public:
    classA();

    inline std::vector<myObject*>* myClass::getVectorPointer(){ return &vectorPointer;}


private:
    std::vector<MyObject*>* vectorPointer;
}

#include "A.h"


int main() {

ClassA myClassAObject;  
std::vector<myObject*>* myStuff = myClassAObject.getVectorPointer();  
MyObject* tempObject = new MyObject("grr");  
myStuff->push_back(tempObject);  

}

yields 402.

Is this just a simple syntax thing, or am I up a creek because pointers to vectors of pointers is just a no no?

I've been trying to see how you'd pass a vector around, and while most things I see show calling a function that receives a &vector and does stuff to the vector in the function, I don't see how that's much different than instead calling a function that returns the reference and then does stuff to the vector there. Is this just a huge logic flaw on my part?

Any clue how I can fix my program to what is proper, very quickly?


I'm going to try the things suggested. You've all been incredibly helpful. Thank you so much!

+1  A: 

In the code above vectorPointer is never assigned / allocated.

Generally it's better to return references rather than pointers as they can't be null.

  Class A.h
#include <vector>
#include "myObject.h"

ClassA{
public:
    classA();

    inline std::vector<myObject*>& myClass::getVectorPointer(){ return vectorPointer;}


private:
    std::vector<MyObject*> vectorPointer;
}
Tom
You might want to re-markup your post
André Caron
Fixed formatting for you. There are many reasons to use references instead of pointers, but that is unfortunately a reason one would use pointers instead of references.
Potatoswatter
*deleted this because it's late and I'm going nuts*
Amber
Amber
@Amber: a reference is just an alias to an object. So referring to the reference is as close as you can get to having the object as a local variable and just using that. In the parlance of the language designers, "the reference is the referent." (On the other hand, you should never return a reference to a local object, because it gets destroyed, and then the caller pretends that it still exists. Returning a reference to a class member as you do here is fine.)
Potatoswatter
Well, it looks like what you're proposing is what I attempted to do but failed. I will see how that goes.Is there any chance it will try to copy my vector a lot if I do it this way?
Amber
Tom
@Potatoswatter - thanks, answered on ipad had swapped to laptop to update.
Tom
A: 

I think you want to make your vectorPointer to be just a vector, not a pointer to a vector. when you return a pointer, you're actually returning a pointer to a pointer.

sizzzzlerz
A: 

There's nothing illegal about vectors of pointers, or pointers to vectors of pointers.

In your code though you're returning the address of a pointer to a vector, rather than the pointer to a vector.

patros
A: 

The C++ containers library (or STL, including vector) can be seen as existing to protect you from pointer errors, and the drudgery of reimplementing data structures. So although what you wrote is legal C++ (it compiled, in any case), it appears that you made some kind of pointer error.

I'd recommend replacing the pointers with plain objects and references wherever possible, to simplify the code and let C++ do the work for you, while (or as a means of) trying to find the pointer bug.

Potatoswatter
My only concern is, isn't there something that can happen where it makes multiple copies of your vectors if you don't pass them the correct way?
Amber
@Amber: For a copy to be made, you need to either declare a new variable (of non-reference type) to receive the copy, or return by value. (And return by value only rarely actually makes a copy; usually you return a local variable which becomes identical to the return value.) Anyway, don't worry about optimizing the program before it works ;v) .
Potatoswatter
get it right first - worry about optimizing for copies only if profiling says you should
jk