Problem with vectors and OOP

Started by
12 comments, last by Lawtonfogle 15 years, 10 months ago
First of all, hello. I am making a RPG game using C++ and DarkGDK. In Java, months ago, I used to make simple games, and while creating vectors, I was able to put "children" classes inside of a "mother-class" vector. Like, if I had a class named "Potion" inherited from a class named "Item", i could create a vector<Item> and put the "Potion" inside of it, by casting Potion to Item. I tried doing a similar thing in C++, but it's not working. I get the error message cannot convert from 'overloaded-function' to 'const Item'. I created a class named WoodenSwd, which is of type MainHand, which is of type Equipment, which is by the way of type Item. The testing code I used: std:: vector <Item> items; ek_WoodenSwd temp1(); items.push_back(temp1); I apologize if it's a stupid question, or if it has been asked before, but, how can I create a vector of type "Item" and insert "Equipment" objects?
Spots of love in a deep and red scarlet...
Advertisement
The problem is, I believe, this line:

ek_WoodenSwd temp1();

The compiler is seeing that as a declaration of a function, not a variable. This line is totally ambiguous with a declaration of a function which takes no arguments and returns an ek_WoodenSwd. Remove the "()", and it should be fine.

BTW, the error message tells you this. It says that it can't convert an "overloaded function". Since you need it to convert temp1, temp1 must be an function. Sometimes the real error is hidden within the error message.
Quote:Original post by FrostyChaotix
I created a class named WoodenSwd, which is of type MainHand, which is of type Equipment, which is by the way of type Item.

Idiomatic C++ doesn't use inheritance in the same way that idiomatic Java does. As such, that kind of deep inheritance hierarchy looks out of place in C++ and is generally considered bad design; I would prefer to confine such practices to Java alone.

Quote:The testing code I used:
std:: vector <Item> items;
ek_WoodenSwd temp1();
items.push_back(temp1);

This will work:

WoodenSword sword;
std::vector<Item *> items;
items.push_back(&sword);

You have to keep in mind though that the sword object has been created on the stack, rather than the heap. You can allocate on the heap using new and delete but then you need to consider who owns the object, the answer is RAII.
Your vector can only contain Items. Polymorfism only work on references. In your case I suggest using a vector of pointers to Items.
Ex)
std::vector<Item*> my_items;

In java all variables are references to objects and therefore you fucked it upp when switching to C++.



Quote:Original post by ApLars
Your vector can only contain Items. Polymorfism only work on references. In your case I suggest using a vector of pointers to Items.
Ex)
std::vector<Item*> my_items;

In java all variables are references to objects and therefore you fucked it upp when switching to C++.


There is some imprecision in your statement. You're using the word 'reference' to mean 'pointer', which it isn't.

In C++ references and pointers are two distinct things. Incidentally both of them support polymorphism/dynamic binding, however both have wildly different semantics beyond that.
Yep don't feel bad, I thought references were pointers as well for an embarrassingly long time.
==============================
A Developers Blog | Dark Rock Studios - My Site
Quote:Original post by fpsgamer
Quote:Original post by ApLars
Your vector can only contain Items. Polymorfism only work on references. In your case I suggest using a vector of pointers to Items.
Ex)
std::vector<Item*> my_items;

In java all variables are references to objects and therefore you fucked it upp when switching to C++.


There is some imprecision in your statement. You're using the word 'reference' to mean 'pointer', which it isn't.

In C++ references and pointers are two distinct things. Incidentally both of them support polymorphism/dynamic binding, however both have wildly different semantics beyond that.


Er, I read it as using "reference" in the language-theoretic sense, but "pointer" in the C++-specific sense. :/
Quote:Original post by Zahlman
Quote:Original post by fpsgamer
Quote:Original post by ApLars
Your vector can only contain Items. Polymorfism only work on references. In your case I suggest using a vector of pointers to Items.
Ex)
std::vector<Item*> my_items;

In java all variables are references to objects and therefore you fucked it upp when switching to C++.


There is some imprecision in your statement. You're using the word 'reference' to mean 'pointer', which it isn't.

In C++ references and pointers are two distinct things. Incidentally both of them support polymorphism/dynamic binding, however both have wildly different semantics beyond that.


Er, I read it as using "reference" in the language-theoretic sense, but "pointer" in the C++-specific sense. :/


Well sometimes I guess I can be awfully pedantic :)
Thanks for the responses, I got it working.

It became:

std::vector<Item *> items;
items.push_back(&WoodenSwd());

WoodenSwd() is the constructor method for the class WoodenSwd.
Spots of love in a deep and red scarlet...
Quote:Original post by FrostyChaotix
Thanks for the responses, I got it working.

It became:

std::vector<Item *> items;
items.push_back(&WoodenSwd());

WoodenSwd() is the constructor method for the class WoodenSwd.


There is a problem with your implementation.

What you're doing is creating a temporary WoodenSwd object, taking its address and pushing that into the vector. After the line containing push_back(), the temporary object is destroyed and you're left with a pointer pointing to the now destroyed object.

Do this instead:
std::vector<Item *> items;items.push_back(new WoodenSwd());

Just remember to call delete on that pointer when you remove it from the vector.

This topic is closed to new replies.

Advertisement