Page 1 of 1

[SOLVED] Class Vector problem...

Posted: Thu Aug 20, 2009 4:26 pm
by Gamma
Hey everyone!
First post on the Elysian Shadow's forums and unfortunately it's for a problem i have using vectors :[. Hope someone can help, here's the low-down...
I'm trying to create a 'vector' of my own defined class 'npc' inorder to create and assign objects using the '.push_back()' function. I first create my vector 'vector<npc> mynpcs;' and then call 'mynpcs.push_back();', but i get an error 'No matching function... blahblah'. It seems to work fine for standard types such as int. Are vectors the direction i should head in order to creaqte 'objects' on the fly and allow easy tracking? Thanks for reading y'all!

p.s. (if i've made a noob mistake here, please feel free to whack me on the head with a shoe horn ;) , i'm new!)

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 4:35 pm
by dandymcgee
Gamma wrote:Hey everyone!
First post on the Elysian Shadow's forums and unfortunately it's for a problem i have using vectors :[. Hope someone can help, here's the low-down...
I'm trying to create a 'vector' of my own defined class 'npc' inorder to create and assign objects using the '.push_back()' function. I first create my vector 'vector<npc> mynpcs;' and then call 'mynpcs.push_back();', but i get an error 'No matching function... blahblah'. It seems to work fine for standard types such as int. Are vectors the direction i should head in order to creaqte 'objects' on the fly and allow easy tracking? Thanks for reading y'all!

p.s. (if i've made a noob mistake here, please feel free to whack me on the head with a shoe horn ;) , i'm new!)
Have you tried:

Code: Select all

vector<npc> mynpcs;
npc jerry;
mynpcs.push_back( jerry );
By the way, depending on the implementation you may want a vector of pointers.

Code: Select all

vector<npc*> mynpcs;
mynpcs.push_back( new npc() );

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 4:37 pm
by Netwatcher
Can you show us the code? it will help analyzing the problem much easier.

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 4:51 pm
by Gamma
Hi, thanks for the fast responses, i'll look into using pointers now 'scratch that, i tried the 'jerry' thing and that did work! However is there a way to create an object that will be added directly onto the back of the vector 'no matter the current size' automatically, i.e i could keep creating npc objects with the click of a button in-game? oh and here's the code...

This is in my main.cpp file:

Code: Select all

vector<npc> mynpcs; 
mynpcs.push_back();
And this is the npc class:

Code: Select all

class npc : public Character
{
    public:
    //Can't use the ':' operator to initialize, due to variables being inherited from 'character' class.
    npc(int hp, int sd, int s, int d, int x, int y)
    {
        health = hp; //So i put them here ^^
        speed = sd;
        str = s;
        defence = d;
        posx = x;
        posy = y;
        gold = 100;
    }
    void setmsg();//Sets the npc's message (for testing xD).

    private:
    string message;
    int gold;
};
Hope this helps in anyway! :][quote][/quote]

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 4:53 pm
by dandymcgee
Gamma wrote:Hi, thanks for the fast responses, i'll look into using pointers now, and here's the code...

This is in my main.cpp file:

Code: Select all

vector<npc> mynpcs; 
mynpcs.push_back();
I edited my post, but for the sake of ease:

You need to pass what you'd like to push back. For Example:

Code: Select all

vector<npc> mynpcs; 
npc jerry;
mynpcs.push_back( jerry );
Try that.

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:00 pm
by Gamma
ah yes, that did work ^^ thank you! :] Would there be a way in order to 'create npc's' where i don't define the name, rather it adds a new one to the vector and 'indexes' it? So i could spawn them whenever i needed, like in-game via a button? Cheers.

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:02 pm
by dandymcgee
Gamma wrote:ah yes, that did work ^^ thank you! :] Would there be a way in order to 'create npc's' where i don't define the name, rather it adds a new one to the vector and 'indexes' it? So i could spawn them whenever i needed, like in-game via a button? Cheers.
Check my first post for a dynamically allocated vector of pointers.

You can create new NPCs during execution by calling new and "push_back"ing the result into the vector. ;)

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:09 pm
by Gamma
'sorry didn't read your last post thorough enough ;),i'm little sleepy :P' but aye, that seems to of worked fine, thanks! On another note, how would i pin-point a certain member in the vector? mynpcs[0] for the first npc? And i take it i need to run through and 'delete' all the 'new' npcs created? thanks!

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:14 pm
by dandymcgee
Gamma wrote:Brilliant, that seems to of worked cheers again :], also, how would i pin-point a certain member in the vector? mynpcs[0] for the first npc? And i take it i need to run through and 'delete' all the 'new' npcs created? thanks!
Yes, and yes. Not sure how good you are with pointers, but just remember that you need to access elements' members with "->" not "."

Code: Select all

mynpcs[0]->Draw();
//instead of
//mynpcs[0].Draw();

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:18 pm
by Gamma
Ah perfect, i think that's all my problems solved :] For now... :lol:, i can now spawn npcs on demand yay! Thanks again for the help!

Re: Class Vector problem...

Posted: Thu Aug 20, 2009 5:20 pm
by dandymcgee
Gamma wrote:Ah perfect, i think that's all my problems solved :] For now... :lol:, i can now spawn npcs on demand yay! Thanks again for the help!
No problem, and welcome to the forums! 8-)

Re: [SOLVED]

Posted: Thu Aug 20, 2009 5:23 pm
by dandymcgee
Not required, but it's generally more beneficial for the community to archive topics with an edited titles as opposed to a replaced title.

"[SOLVED] Class Vector problem..." as opposed to "[SOLVED]" helps with search results. ;)

Edit: Suppose it doesn't matter that much since it's plastered so many times elsewhere in this topic. :lol: