• Advertisement

Archived

This topic is now archived and is closed to further replies.

C++ Problem

This topic is 5827 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

Hi, I''m a beginner C++ programmer, and I''m having this problem, I hope someone can help I have 2 classes, Warrior, and Rogue. Here''s some code to help describe my problem:
void createCharacter()
{
  cout << "Choose a character class: \n";
  cout << "1) Warrior \n";
  cout << "2) Rogue\n";
  int input;
  if(input == 1) {
  createWarrior();
  }
  else if(input == 2) {
  createRogue();
  }
  else {
  cout << "Enter 1 or 2.\n";
  system("cls");
  createCharacter();
  }
}

// I''ll skip createRogue for space-saving.
void createWarrior()
{
  Warrior *Khan = new Warrior;
  cout << "Khan''s strength is: " << Khan->getStr() << endl;
  // etc.
}

 
Anyway. If I have another function, say:
beginFirstScene()
{
  int health_remaining = Khan->getHealth();
  if(health_remaining == 0) 
  {
    Khan->Die();
   // This function plays a cool sound using FMOD 
  }
}
 
But the compiler (Dev-C++), says things like Khan: undefined, first use in this function. etc etc. I need a way to, when the user selects the ''class'', a way of setting their class as a global variable, so all functions can call, say Khan->Die(); etc. I''ve tried things like: #define KHAN Warrior *Khan = new Warrior; void some_function() { KHAN->Die(); } But that doesn''t work. =/ /me is very stuck. Help is much appreciated. Thanks, renegade

Share this post


Link to post
Share on other sites
Advertisement
Character Khan;

int main(){}


In other words simply define khan outside of any functions like so

#include "Character.h"

Character Khan;

int main()
{
if ( Khan.Gethealth() < 1 )
Khan.Die();

return 0;
}

~Vendayan

Share this post


Link to post
Share on other sites
Thank you

You make it sound so simple (well it is, but I was trying to looking for a hard way around it, instead of a simple way..)

I always forget there''s always numerous simple ways to do things, but I just never think.
Enough of my blabbering, I can get back to code now.

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by renegade_

void createCharacter()
{
cout << "Choose a character class: \n";
cout << "1) Warrior \n";
cout << "2) Rogue\n";
int input;
////////////////////////////////<--------
if(input == 1) {
createWarrior();
}
else if(input == 2) {
createRogue();
}
else {
cout << "Enter 1 or 2.\n";
system("cls");
createCharacter();
}
}




Shouldn''t you, oh, I dunno, input Input?

cin >> input;

"I''ve learned something today: It doesn''t matter if you''re white, or if you''re black...the only color that REALLY matters is green"
-Peter Griffin

Share this post


Link to post
Share on other sites

  • Advertisement