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!