[SOLVED] Class Vector problem...
Moderator: Coders of Rage
- Gamma
- 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...
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!)
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
- dandymcgee
- 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...
Have you tried: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!)
Code: Select all
vector<npc> mynpcs;
npc jerry;
mynpcs.push_back( jerry );
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!
- Netwatcher
- 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...
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/
-Derek Powazek, http://powazek.com/posts/1655
blip.fm DJ profile - http://blip.fm/Noobay
current code project http://sourceforge.net/projects/vulcanengine/
- Gamma
- 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...
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:
And this is the npc class:
Hope this helps in anyway! :][quote][/quote]
This is in my main.cpp file:
Code: Select all
vector<npc> mynpcs;
mynpcs.push_back();
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;
};
Last edited by Gamma on Thu Aug 20, 2009 4:57 pm, edited 1 time in total.
awesome sig
- dandymcgee
- 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...
I edited my post, but for the sake of ease: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();
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 );
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Gamma
- 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...
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
- dandymcgee
- 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...
Check my first post for a dynamically allocated vector of pointers.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.
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!
- Gamma
- 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...
'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
- dandymcgee
- 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...
Yes, and yes. Not sure how good you are with pointers, but just remember that you need to access elements' members with "->" not "."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!
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!
- Gamma
- 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...
Ah perfect, i think that's all my problems solved :] For now... , i can now spawn npcs on demand yay! Thanks again for the help!
awesome sig
- dandymcgee
- 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...
No problem, and welcome to the forums!Gamma wrote:Ah perfect, i think that's all my problems solved :] For now... , i can now spawn npcs on demand yay! Thanks again for the help!
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- dandymcgee
- 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]
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.
"[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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!