To create one object i do the following:
ClassName *menu;
menu = new ClassName("images/menu.bmp", 1);
im having trouble creating a vector of these objects.
creating a vector seems to work:
vector %26lt;ClassName%26gt; *vecSouth = new vector%26lt;ClassName%26gt;;
or
vector %26lt;ClassName%26gt; vecNorth();
but when i try to create an object in the vector by using push_back i get errors, im not sure how to do it.
ive tried:
vecSouth.push_back("images/menu.bmp", 1);
vecSouth.push_back(new CLassName("images/menu.bmp", 1));
etc. none works. i allways get the error:
error C2228: left of '.push_back' must have class/struct/union type
any help would be great. thx
both work
Creating a vector of objects in C++ with visual C++ v6?
Your problem is that you are creating a vector of objects, when you really want to create a vector of pointers to those objects:
vector %26lt;ClassName *%26gt; vecSouth;
Then the following should work:
vecSouth.push_back(new CLassName("images/menu.bmp", 1));
since the new is returning a pointer to a ClassName. The only way to make your original version work would be to dereference the new'ed value.
vecSouth-%26gt;push_back(*(new ClassName.....));
Note that it's usually preferable to have your containers store pointers to things vs copies of things (to avoid having multiple copies of objects and to avoid any confusion/ambiguity about exactly what is being stored. Though you could have a reason to want to store copies, again, just be sure you're straight in your mind as to exactly what you want to do.
land survey
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment