[SOLVED] Class Vector problem...

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Gamma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sun Aug 16, 2009 8:21 pm
Programming Language of Choice: C++
Location: United Kingdom

[SOLVED] Class Vector problem...

Post 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!)
Last edited by Gamma on Thu Aug 20, 2009 5:26 pm, edited 2 times in total.
awesome sig
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class Vector problem...

Post 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() );
Last edited by dandymcgee on Thu Aug 20, 2009 4:51 pm, edited 3 times in total.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Netwatcher
Chaos Rift Junior
Chaos Rift Junior
Posts: 378
Joined: Sun Jun 07, 2009 2:49 am
Current Project: The Awesome Game (Actual title)
Favorite Gaming Platforms: Cabbage, Ground beef
Programming Language of Choice: C++
Location: Rehovot, Israel

Re: Class Vector problem...

Post by Netwatcher »

Can you show us the code? it will help analyzing the problem much easier.
"Programmers are the Gods of their tiny worlds. They create something out of nothing. In their command-line universe, they say when it’s sunny and when it rains. And the tiny universe complies."
-Derek Powazek, http://powazek.com/posts/1655

blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
User avatar
Gamma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sun Aug 16, 2009 8:21 pm
Programming Language of Choice: C++
Location: United Kingdom

Re: Class Vector problem...

Post 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]
Last edited by Gamma on Thu Aug 20, 2009 4:57 pm, edited 1 time in total.
awesome sig
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class Vector problem...

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Gamma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sun Aug 16, 2009 8:21 pm
Programming Language of Choice: C++
Location: United Kingdom

Re: Class Vector problem...

Post 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.
awesome sig
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class Vector problem...

Post 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. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Gamma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sun Aug 16, 2009 8:21 pm
Programming Language of Choice: C++
Location: United Kingdom

Re: Class Vector problem...

Post 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!
Last edited by Gamma on Thu Aug 20, 2009 5:15 pm, edited 1 time in total.
awesome sig
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class Vector problem...

Post 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();
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Gamma
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Sun Aug 16, 2009 8:21 pm
Programming Language of Choice: C++
Location: United Kingdom

Re: Class Vector problem...

Post 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!
awesome sig
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Class Vector problem...

Post 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-)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: [SOLVED]

Post 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:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply