Can you create a Vector of structs in C++? I need help with a unknown error pertaining to this subject.

Started by
21 comments, last by uglybdavis 11 years, 4 months ago
CatmanFS: Not quite sure, what you mean, but if you want to have a vector<int*>'s element point to instances of anything different than ints, you will have to use an explicit cast. This will work maybe, but is more or less one of the most evil things to do.

Also, as Cornstalks already said, using raw pointers instead of smart pointer creates a lot of potential problems later on.

Edit: you ninja, BitMaster
Advertisement

May i suggest (read the comments, i changed them:

[source lang="cpp"]int main() {
Point tl(100,100);
Simple_window win(tl,400,400,"My Window");
vector<Rectanglee*> rects; // Pointer Magic!

for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
rects.push_back(new Point(i,j),50,50);
}
}
for(int i=0;i<64;i++){
win.attach(*(rects)); //Remember to de-reference
}
win.wait_for_button(); //wait until user clicks the next button to close window

// Clean up at some point!
for (unsigned int i = 0, size = rects.size(); i < size; ++i)
delete rects;
}[/source]


I read this article http://www.cplusplus...orial/pointers/ . And from what I understand making rects a pointer would make it incapable of using push_back which is the whole reason I am using a vector in the first place.

I suppose if I did something like this. It might work.


for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
(*rects).push_back(new Point(i,j),50,50);
}
}
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().

Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().


So something like this. I have never used smart pointers. But I will look into it.


for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}
You shouldn't store a pointer to an object on the stack in a vector. Use new to create your object and store that pointer instead.

[quote name='SiCrane' timestamp='1355333123' post='5009892']
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().


So something like this. I have never used smart pointers. But I will look into it.


for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}

[/quote]

that is a crash man. You cannot grab the address of a stack variable like that just as you cannot return a pointer to a variable declared into a function. Once it goes out of scope you'll be pointing at garbage memory.

rects.push_back(new Rectangle(bbla bla bla));

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


[quote name='black_darkness' timestamp='1355333417' post='5009894']
[quote name='SiCrane' timestamp='1355333123' post='5009892']
Don't make rects a pointer, make it a vector containing pointers. Preferably smart pointers, but regular pointers would also work. Either way it's legal to store both regular and smart pointers inside a vector, including using push_back().


So something like this. I have never used smart pointers. But I will look into it.


for (int i=0;i<8;i++){
int xa=(i*50)-1;
for(int j=0;j<8;j++){
int ya=(j*50)-1;
Rectanglee rect(Point(i,j),50,50);
rects.push_back(&rect);
}
}

[/quote]

that is a crash man. You cannot grab the address of a stack variable like that just as you cannot return a pointer to a variable declared into a function. Once it goes out of scope you'll be pointing at garbage memory.

rects.push_back(new Rectangle(bbla bla bla));
[/quote]


I tried that, but it says Rectanglee has no default constructor. I can see now that this problem is too advanced for me. I give up now. Thanks for the help.
@black_darkness

[source lang="cpp"]vector<SomeData>* pVector; // Pointer to a vector
pVector = new Vector<SomeData(); // Make new vector
pVector->push_back(something);
(*pVector).push_back(something);
delete pVector; // Delete the vectors

vector<SomeData*> Vector; // Vector storing pointers
Vector.push_back(new SoemData()); // Make new data
delete Vector[0]; // Delete the elements

vector<SomeData*>* pVector; // Vector pointer to pointers
pVector = new vector<SomeData*>(); // Make the data structure
pVector->push_back(new SomeData()); // Make new data
(*pVector).push_back(new SomeData()); // Just syntax difference
delete pVector[0]; // Delete elements first
delete pVector[1]; // We added two elements
delete pVector; // Delete the vector[/source]

Seems like you are confused about what a vector is / how memory is managed. Also seems that you don't fully understand what the stack / heap are. The beauty of C++ is that YOU are responsible for the memory. Forget about all the fancy smart pointer work, you use C++ in games for the speed, don't add overhead. Learn how to manage your memory. Seriously, learn memory management it's a very important skill.

Does the code sample above shed some light as to what's going on?
i think you forgot the "bbla bla bla" part tongue.png

There you go:

vector<Rectangle*> rects;

rects.push_back(new Rectangle(Point(i,j),50,50));

This will work.. but I agree with you, it's a good thing for you to step back and get more familiar with C++ .

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


@black_darkness

[source lang="cpp"]vector<SomeData>* pVector; // Pointer to a vector
pVector = new Vector<SomeData(); // Make new vector
pVector->push_back(something);
(*pVector).push_back(something);
delete pVector; // Delete the vectors

vector<SomeData*> Vector; // Vector storing pointers
Vector.push_back(new SoemData()); // Make new data
delete Vector[0]; // Delete the elements

vector<SomeData*>* pVector; // Vector pointer to pointers
pVector = new vector<SomeData*>(); // Make the data structure
pVector->push_back(new SomeData()); // Make new data
(*pVector).push_back(new SomeData()); // Just syntax difference
delete pVector[0]; // Delete elements first
delete pVector[1]; // We added two elements
delete pVector; // Delete the vector[/source]

Seems like you are confused about what a vector is / how memory is managed. Also seems that you don't fully understand what the stack / heap are. The beauty of C++ is that YOU are responsible for the memory. Forget about all the fancy smart pointer work, you use C++ in games for the speed, don't add overhead. Learn how to manage your memory. Seriously, learn memory management it's a very important skill.

Does the code sample above shed some light as to what's going on?


At this moment no. That probably has to do with a head-ache I have at the moment. I will come back to this thread and post when the head-ache goes away. Thanks for the help and excuse me for all the trouble.

This topic is closed to new replies.

Advertisement