How to create objects outside of the main function!

Everything runs fine, but my problem is that my main function is too long. Is there a way to create the battle scene outside of the main function? I have tried this, but if i put the battle scene-lets say "Battle()"-In a header file or something, then my objects create an error. Is there any way to make the objects i declare in main work outside the main function. Maybe using pointers or something??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
     int main()
        {
        	Hero Me(100,20,30,40);//Created using overloaded constructor
        	Monster m(100,16,18,20);//creates a monster object and
 uses overloaded constructor to initialize
        
    	cout << "\nAttacking!\n";

    //I want this part to be in a function called Battle() outside of main
    	while ((Me.getHp() > 0) && (m.getHp() > 0))
//Generates error if object not declared in main
    	{	
    		cout << "\nYour hp is: " << Me.getHp() << endl;
    		cout << "The enemy's hp is: "<< m.getHp() << endl;
    		cout << "\nThe monster has attacked you!\n";
    		cout << "You received " << m.getAttack() << " damage;" << endl;
    		Me.damageTaken(m.getAttack());
//Me.setHp(Me.getHp() - m.getAttack());//Me.setHp(m.getStrength());
    		if(Me.getHp() > 0)//Check if still alive
    		{
    			cout << "\nYour hp is now: " << Me.getHp() << endl;
    			//cout << "Enemy hp is: "<< m.getHp() << endl;
    			cout << "\nNow you attacked!\nYou have dealt "<< Me.getAttack()
 << " Damage" << endl;
    			m.damageTaken(Me.getAttack());
//m.setHp(m.getHp() - Me.getAttack());//m.setHp(Me.getAttack());
    
    			if(m.getHp() > 0)//Check if still alive
    			{
    				cout << "Enemy hp is now: " << m.getHp() << endl;
    				cout << "\nAttacking again!\n";
    			}
    		}
    
    	} 
    		if ((Me.getHp() > 0) && (m.getHp() <= 0))
    				cout <<"\nCongratulations! You killed the enemy!" << endl;
    
    		else if ((Me.getHp() <= 0) && (m.getHp() > 0))
    				cout << "You have died!" << endl;
    	
    
    
    
    	cin.sync();
    	cin.get();
    	return 0;
    }


Heres the rest if the code:
1
2
3
4
5
6
7
8
9
10
11
    //Hero.h
    class Hero:
    	public Character
    {
    public:
    	Hero();
    	Hero(int, int, int, int);
    	~Hero();
    
    };
    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    //Hero.cpp
    Hero::Hero(int newHp, int newLevel, int newAttack, int newDef)
        : Character(newHp, newLevel, newAttack, newDef)
    {
    	cout << "Hero created using Overloaded function!\n";
    	hp = newHp;
    	cout << "Hp is: "<< hp << endl;
    	level = newLevel;
    	cout << "level is: " << level << endl;
    	attack = newAttack;
    	cout << "Attack is: " << attack << endl;
    	defense = newDef;
    	cout << "Defense is: " << defense << endl;
        // logging goes here
        // note that you don't need HeroLevel etc. at all any more, just use level
    }

    Hero::~Hero()
    {
    	cout << "Hero destroyed!\n";
    }


1
2
3
4
5
6
7
8
9
10
11
12
    //Monster.h
    class Monster:
    	public Character //Hero
    {
    public:
    	Monster();
    	Monster(int, int, int, int); //explicit
    		//:Character(){};
    	//Monster(int);
    	~Monster();
    };
    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    //Monster.cpp
    Monster::Monster(int newHp, int newLevel, int newAttack, int newDef)
    	//: hp(newHp), level(newLevel), attack(newAttack), defense(newDef)//initialize list
    {
    	cout << "Monster created using Overloaded function!\n";
    	hp = newHp;
    	cout << "Hp is: "<< hp << endl;
    	level = newLevel;
    	cout << "level is: " << level << endl;
    	attack = newAttack;
    	cout << "Attack is: " << attack << endl;
    	defense = newDef;
    	cout << "Defense is: " << defense << endl;
    }
    
    Monster::~Monster()
    {
    	cout << "\nMonster Destroyed";
    }
    


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
    //Character.h
    class Character
    {
    
    protected://not private, so derived class can use it
    	int level;
        int hp;
        int attack;
        int defense;
        Character(); // zero everything by default
        Character(int); // randomly generate everything
        Character(int, int, int, int); // populate explicitly
    	~Character();
    public:
        int getAttack() const { return attack; }
    	int getDefense() const { return defense; }
    	int getHp() const { return hp; }
    	int getlevel() const { return level; }
    
    	void setAttack(int);
    	void setDefense(int);
    	void setStrength(int);
    	void setHp(int);
    	void setlevel(int);
    	void damageTaken(int);
    	
    //Character.cpp
    Character::Character() : level(0), hp(0), attack(0), defense(0) {}
    
    Character::Character(int hit, int lvl, int att, int def)
     : level(lvl), hp(hit), attack(att), defense(def){}
    
    
    Character::~Character()
    {
    	cout << "Character has been destroyed!\n";
    }
Call a function inside main which will handle the entire battle for you. ex:

void BattleNow()
yes i tried doing that, but it didn't work. It compiled a bunch of errors! the problem was with the objects. As soon as i take out my object, which in this case is Hero Me and Monster m , and put it in a function outside main it compiles errors saying identifier(Me and m) is undefined.

For example:
1
2
3
//Main
Hero Me(1,2,3,4);
Monster m(5,6,7,8);


1
2
3
4
5
6
7
8
9
10
11
12
13
//Characters.h
public:
void Battle();

//Characters.cpp
Void Battle()
{
while ((Me.getHp() > 0) && (m.getHp() > 0))//generates error
{
     cout << "Battle";
}

}
The Me and m in your main file don't exist in the Characters files. You should just pass those as parameters to the Battle function.
so can i create another header or something and declare the objects there? and do you mean pass the objects as parameters?
do you mean pass the objects as parameters?

Right, that's exactly it. You should try to avoid just making the objects global.
but how would i even make a prototype of objects? Void battle(Hero); or something? i Know this is totally wrong.
Topic archived. No new replies allowed.