Jump to content
  • Advertisement
Sign in to follow this  
FrostyChaotix

Problem with vectors and OOP

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

If you intended to correct an error in the post then please contact us.

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 this post


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

Share this post


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

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
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. :/

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!