开发者

Passing Pointer of vector of pointers... no no?

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

The actual code is on a different computer and I can't c开发者_StackOverflowopy 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!


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;
}


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜