Thread: Undeclare Variable????

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    56

    Undeclare Variable????

    Hi everybody,

    i am creating my very first little game now, it is just the beginning of the game...however i found an error while compiling:

    'obj' : undeclare identifier
    it was in line 85.

    Here is my program....:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef char string[25];
    
    class Digimon
    {
    private:
    	string name;
    	int health;
    	int stamina;
    public:
    	Digimon(string="nothing", int=0, int=0);
    	friend ostream& operator<<(ostream& out, Digimon& digi);
    friend void Play(Digimon, Digimon);
    void AttLost(int); 
    };
    
    
    Digimon::Digimon(string n, int hp, int sp)
    {
    	strcpy(name,n);
    	health=hp;
    	stamina=sp;
    }
    
    ostream& operator<<(ostream& out, Digimon& digi)
    {
    	out<<"Your Digimon STATS"<<endl;
    	out<<"Your Digimon : "<<digi.name<<endl;
    	out<<"Your HP : "<<digi.health<<endl;
    	out<<"Your SP : "<<digi.stamina<<endl;
    	return out;
    }
    
    void Play(Digimon enem, Digimon you)
    {
        while ((enem.health==0)||(you.health==0))
        {
         cout<<you.name<<" ATTACK!!!"<<endl;
         enem.AttLost(2);
         cout<<enem.name<<" WOOF!!"<<endl;
         you.AttLost(1);
        }
        if (enem.health<=0)
        {
           cout<<"YOU WIN!!";
        }
        else
            cout<<"YOU LOST!!";
    }
     
    
    void Digimon::AttLost(int lostHP)
    {
         health=health-lostHP;
    }
    
    void ChooseCharacter(int&);         //Function prototype
    
    int main()
    {
    	int ans;
    	ChooseCharacter(ans);
    	if (ans==1)
    	{
    		Digimon obj("Guilmon", 25, 10);
    		cout<<obj;
    	}
    	else if (ans==2)
    	{
    		Digimon obj("Renamon", 20, 20);
    		cout<<obj;
    	}
    	else
    	{
    		Digimon obj("Terriermon", 22, 15);
    		cout<<obj;
    	}
        cout<<"You encounter a crazy stupid yet powerful digiMONSTER!!"<<endl;
        cout<<"IT is RUBYMON!!!"<<endl;
        Digimon enemy1("Rubymon", 8, 4);
        Play(enemy1,obj);                            //LINE 85!!! ERROR HERE!!
        
        cout<<enemy1;
        system("PAUSE");
    	return 0;
    }
    
    void ChooseCharacter(int& ans)
    {
    	cout<<"Welcome to The Digital World"<<endl;
    	cout<<"Please choose your favorite digimon to travel with you!"<<endl;
    	cout<<"1.Guilmon"<<endl;
    	cout<<"2.Renamon"<<endl;
    	cout<<"3.Terriermon"<<endl;
    	cin>>ans;
    }
    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You declare obj inside of an if block, this is a separate scope and obj does not exist outside of it.
    My best code is written with the delete key.

  3. #3
    Grammar Police HybridM's Avatar
    Join Date
    Jan 2003
    Posts
    355
    lol prelude, love the Avatar....first time i've seen it (sorry if it's old).
    Thor's self help tip:
    Maybe a neighbor is tossing leaf clippings on your lawn, looking at your woman, or harboring desires regarding your longboat. You enslave his children, set his house on fire. He shall not bother you again.

    OS: Windows XP
    Compiler: MSVC

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    56
    ok..thanks, but what can i do to fix this problem??

    Can i declare it outside the function and then create another "method function" call "setvalue" and call it inside the "if".

    Ex:

    class Digimon
    {
    ......
    public:
    Digimon(string="nothing", int=0, int=0); //constructor
    void setvalue(string, int, int); //new method function
    }
    ...
    void setvalue (string n, int hp, int sp)
    {
    strcpy(name, n);
    health=hp;
    stamina=sp;
    }
    ..

    ...
    int main()
    {
    Digimon obj;
    if (ans==1)
    obj.setvalue(...........);

    etc
    etc
    etc
    }


    I'm not sure if i can do that...

  5. #5
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    That is a perfectly valid solution, there is nothing wrong with doing that especially considering that the object is created regardless of the result of the "if" statement. It's just the initial values that change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM