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.
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!)
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
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...
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.
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.
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!
'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.
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 "."