Public Group

Problem with vectors and OOP

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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?

Share on other sites
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.

Share on other sites
Quote:
 Original post by FrostyChaotixI 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 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.

Share on other sites
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++.

Share on other sites
Quote:
 Original post by ApLarsYour 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 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.

Share on other sites
Yep don't feel bad, I thought references were pointers as well for an embarrassingly long time.

Share on other sites
Quote:
Original post by fpsgamer
Quote:
 Original post by ApLarsYour 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 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. :/

Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by fpsgamer
Quote:
 Original post by ApLarsYour 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 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 :)

Share on other sites
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.

Share on other sites
Quote:
 Original post by FrostyChaotixThanks for the responses, I got it working.It became:std::vector 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.

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

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

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
17

• 10
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631420
• Total Posts
2999987
×