Advertisement

C++ Problem

Started by February 02, 2002 07:07 AM
3 comments, last by renegade_ 22 years, 9 months ago
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
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
"Never have a battle of wits with an unarmed man. He will surely attempt to disarm you as well"~Vendayan
Advertisement
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
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
"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
You''re right, I just wrote that code quickly to illustrate my problem, and I made a mistake

This topic is closed to new replies.

Advertisement