[SOLVED] Accessing a class in another File

Hey guys well my first post here but Ill get straight to my problem.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//main.cpp
#include "startup.h"

int main()
{
	StartUp();
	return 0;
}
//-------------------------


//character_class.h
#ifndef _CHARACTER_CLASS_H
#define _CHARACTER_CLASS_H

class Character
{
	public:
		Character();
		//~Character();
		//X and Y Location
        int iX;
        int iY;
		//Stats
		int health;
		int damage;
		//Boundries
		int iPlayAreaTop;
		int iPlayAreaBottom;
		int iPlayAreaRight;
		int iPlayAreaLeft;
};

Character::Character()
{
	iPlayAreaTop = 0;
	iPlayAreaBottom = 30;
	iPlayAreaRight = 100;
	iPlayAreaLeft = 0;
}

#endif //_CHARACTER_CLASS_H       
//-------------------------


//startup.h
#ifndef _STARTUP_H
#define _STARTUP_H

void StartUp();

#endif //_STARTUP_H
//-------------------------


//startup.cpp
#include <iostream>
#include "startup.h"
#include "character_class.h"
	Character GoodTeam;
	Character *pGoodTeam = &GoodTeam;
void StartUp()
{
        //Test
	std::cout << GoodTeam.iPlayAreaBottom << std::endl;
	pGoodTeam->iPlayAreaBottom -= 10;
	std::cout << GoodTeam.iPlayAreaBottom << std::endl;
}
//-------------------------- 


Ok so mainly my problem lies here - i want to access the class i created called GoodTeam in another file but I'm not sure how to do so. I want to be able to edit its values in another file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//---------------------------

//createarmy.h
#ifndef _CREATEARMY_H
#define _CREATEARMY_H

void Function();

#endif //_CREATEARMY_H
//---------------------------


//createarmy.cpp
#include "createarmy.h"
#include "startup.h"

void Function()
{	
	//Edit GoodTeam here.
}
//--------------------------- 

Well basically where i said "//Edit GoodTeam here." i want to be able to put some code there to pull out GoodTeam from the startup.cpp and edit its values in here. But well basically I have no idea how i have tried a few things but i just get lost and get a stack of linker errors.

Instead of "Void Function()" I was previously thinking it might of looked something like this..
1
2
3
4
5
6
7
8
9
GoodTeam *CreateArmy()
{
	GoodTeam *myCreatedArmy = new GoodTeam[100];
        for(int i = 0; i < 100; i++)
        {
               myCreatedArmy.health = 100;
        }
	return myCreatedArmy;
}

Then changing the createarmy.h as well to this method and having startup.cpp say
GoodArmy = CreateArmy();

If you can make any scence of what im saying.

Cheers guys
EDIT: Added Code Tags
Last edited on
Well, you should be doing all of your class editing/such inside of the "startup" function. Generally the class has functions that allow you to edit it's parameters, etc. If you want to define a function somewhere else that works, make the class as a parameter and then you can pass the class that you want to edit as a parameter.

Also, use [code][/code] tags around your code next time.
Last edited on
Thank you for that reply draco and the heads up on the code tag. Didn't relise you could do that on forums. I understand what you said but my only problem is lets say I do that. I understand how all of the code should be layed out but i always end up with linker errors. So how am I meant to get createarmy.cpp to see the character_class.h as well?

Thanks
A simple method of working with multiple class objects in a single file is to write your class framework into header files... then all you need to do is include the appropriate header files into your .cpp and define your variables. Have a look into data-structures because they're a really good way of seeing standardised ways of organising projects.
I agree but see how I have the "character_class.h" - Lets say i include it into "startup.cpp" Thats fine i can get what ever i need from it.. But then lets say i want to access it in "createarmy.cpp" I'll get a linker error just from incuding it. Thus why I'm a tad lost with what I'm doing. And basically thats the main error i need to fix. Once i get that working it'l be smooth sailing from there.

Cheers
It's actually a common problem, use header protection to fix it:

1
2
3
4
#ifndef HEADER_H
#define HEADER_H
//code
#endif 


Put it in each .h file (changing the define) and it should work fine.
All my header files already have that protection don't they?
Updated code what my actual code is now and the error i have. Might be easier to fix like this..
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//main.cpp
#include "startup.h"

int main()
{
	StartUp();
	return 0;
}
//----------------------------------

#ifndef CHARACTER_CLASS_H
#define CHARACTER_CLASS_H

class Character
{
	public:
		Character();
		//~Character();
		//X and Y Location
        int iX;
        int iY;
		//Stats
		int health;
		int damage;
		//Boundries
		int iPlayAreaTop;
		int iPlayAreaBottom;
		int iPlayAreaRight;
		int iPlayAreaLeft;
};

Character::Character()
{
	iPlayAreaTop = 0;
	iPlayAreaBottom = 30;
	iPlayAreaRight = 100;
	iPlayAreaLeft = 0;
}

#endif //_CHARACTER_CLASS_H
//--------------------------------------------

//startup.cpp
#include <iostream>
#include "startup.h"
#include "createarmy.h"

	Character GoodTeam;					//Create an object of the class
	Character *pGoodTeam = &GoodTeam;	//Create a pointer to the address of GoodTeam

void StartUp()
{
	std::cout << GoodTeam.iPlayAreaBottom << std::endl;
	pGoodTeam->iPlayAreaBottom -= 10;
	std::cout << GoodTeam.iPlayAreaBottom << std::endl;
}
//------------------------------------------------------------

//startup.h
#ifndef STARTUP_H
#define STARTUP_H

void StartUp();

#endif //_STARTUP_H
//------------------------------------------------------------

//createarmy.cpp
#include "createarmy.h"
void CreateArmy(Character *pCharacter)
{	
	pCharacter->health = 100;
	pCharacter->damage = 25;
}
//-------------------------------------------------------------

//createarmy.h
#include "character_class.h"
#ifndef CREATEARMY_H
#define CREATEARMY_H

void CreateArmy(Character *pCharacter);

#endif //_CREATEARMY_H
//--------------------------------------------------- 


Ok so thats the current code and this is the output I'm getting:

1>------ Rebuild All started: Project: AIE_Game_For_Portfolio, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'AIE_Game_For_Portfolio', configuration 'Debug|Win32'
1>Compiling...
1>startup.cpp
1>main.cpp
1>createarmy.cpp
1>Generating Code...
1>Linking...
1>startup.obj : error LNK2005: "public: __thiscall Character::Character(void)" (??0Character@@QAE@XZ) already defined in createarmy.obj
1>startup.obj : error LNK2005: "public: __thiscall Character::Character(void)" (??0Character@@$$FQAE@XZ) already defined in createarmy.obj
1>C:\Documents and Settings\Scottie\My Documents\Visual Studio 2005\Projects\AIE_Game_For_Portfolio\Debug\AIE_Game_For_Portfolio.exe : fatal error LNK1169: one or more multiply defined symbols found
1>Build log was saved at "file://c:\Documents and Settings\Scottie\My Documents\Visual Studio 2005\Projects\AIE_Game_For_Portfolio\AIE_Game_For_Portfolio\Debug\BuildLog.htm"
1>AIE_Game_For_Portfolio - 3 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========


Cheers guys, sorry for probably not explaining this properly but I'm really stumped why I can't get this working right :(
For every .h file you should have a .cpp file. The .h file is where you declare the classes, variables etc, and the .cpp is where you put the code. Ex: Make a Character Class .cpp file and move the definition of the constructor into the .cpp
I'm Gathering you mean something like
1
2
3
4
5
6
7
8
9
10
//character_class.cpp
#include "character_class.h"

Character::Character()
{
	iPlayAreaTop = 0;
	iPlayAreaBottom = 30;
	iPlayAreaRight = 100;
	iPlayAreaLeft = 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//character_class.h
#ifndef CHARACTER_CLASS_H
#define CHARACTER_CLASS_H

class Character
{
	public:
		Character();
		//~Character();
		//X and Y Location
        int iX;
        int iY;
		//Stats
		int health;
		int damage;
		//Boundries
		int iPlayAreaTop;
		int iPlayAreaBottom;
		int iPlayAreaRight;
		int iPlayAreaLeft;
};

Character GoodTeam;
#endif //_CHARACTER_CLASS_H 


It will compile like that but how do i access GoodTeam - as if i #include "character_class.h" anywhere I'll get a linker error :(

I would think you would access it like:
1
2
3
4
5
6
7
8
9
10
11
//startup.cpp
#include <iostream>
#include "startup.h"
#include "character_class.h"

Character *pGoodTeam = &GoodTeam;

void StartUp()
{
        //do something	
}


But if i try anything like this I'll just get the link error: "1>Linking...
1>character_class.obj : error LNK2005: "class Character GoodTeam" (?GoodTeam@@3VCharacter@@A) already defined in startup.obj
1>C:\Documents and Settings\Scottie\My Documents\Visual Studio 2005\Projects\AIE_Game_For_Portfolio\Debug\AIE_Game_For_Portfolio.exe : fatal error LNK1169: one or more multiply defined symbols found"
Last edited on
Hm...I'm not completely sure why it is doing that...try moving GoodTeam and pGoodTeam inside of StartUp and see if that fixes it.
Sadly enough I'm getting the exact same error. Logically it makes scence what I'm trying to do but i just cant figure out why it's not working. I've done this stuff before about a year ago - but since then i had been working with c# and java. So personally I'm so lost as to why I can't do something I've done before. Obviously I'm doing it wrong in some way but god its getting frustrating. I know there are more ways of doing it but as this isn't for me it's for something I need for a portfolio of mine - it has to be done in this way. Bah if anyone has any ideas to something simular please shout out heh.

Cheers for your help firedraco ;)
Hey guys just giving the update that I fixed the program its in a lot more depth now but I have a version from when I got it working just incase any of use wanted to know how I fixed it - so here it goes:

1
2
3
4
5
6
7
8
//main.cpp
#include "startup.h"

int main()
{
	StartUp();
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//character_class.h
#ifndef CHARACTER_CLASS_H
#define CHARACTER_CLASS_H

class Character
{
	public:
		Character();
		//~Character();
		//X and Y Location
        int iX;
        int iY;
		//Stats
		int health;
		int damage;
		//Boundries
		int iPlayAreaTop;
		int iPlayAreaBottom;
		int iPlayAreaRight;
		int iPlayAreaLeft;
};
#endif //_CHARACTER_CLASS_H 


1
2
3
4
5
6
7
8
9
10
//character_class.cpp
#include "character_class.h"

Character::Character()
{
	iPlayAreaTop = 0;
	iPlayAreaBottom = 30;
	iPlayAreaRight = 100;
	iPlayAreaLeft = 0;
}


1
2
3
4
5
6
7
//startup.h
#ifndef STARTUP_H
#define STARTUP_H

void StartUp();

#endif //_STARTUP_H 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//startup.cpp
#include <iostream>
#include "startup.h"
#include "character_class.h"
#include "createarmy.h"
#include "displayarmy.h"

void StartUp()
{
	//Create the two Teams
	Character *GoodTeam, *EvilTeam;
	int numberOfSoldiers = 20;
	GoodTeam = pCreateTeam(numberOfSoldiers);
	EvilTeam = pCreateTeam(numberOfSoldiers);

	//Display the two Teams
	DisplaySoldiers(GoodTeam, numberOfSoldiers);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//createarmy.cpp
#include "createarmy.h"
#include "character_class.h"

Character *pCreateTeam(int &rNumberOfSoldiers)
{
	Character *pMyCreatedArmy = new Character[rNumberOfSoldiers];

	for(int i = 0; i < rNumberOfSoldiers; i++)
	{
		pMyCreatedArmy[i].health = 100;
	}

	return pMyCreatedArmy;
}


1
2
3
4
5
6
7
8
//createarmy.h
#include "character_class.h"
#ifndef CREATEARMY_H
#define CREATEARMY_H

Character *pCreateTeam(int &rNumberOfSoldiers);

#endif //_CREATEARMY_H 


1
2
3
4
5
6
7
8
//displayarmy.h
#ifndef DISPLAYARMY_H
#define DISPLAYARMY_H
#include "character_class.h"

void DisplaySoldiers(Character *pMyCreatedArmy, int numberOfSoldiers);

#endif //DISPLAYARMY_H 


1
2
3
4
5
6
7
8
9
10
11
12
//displayarmy.cpp
#include <iostream>
#include "displayarmy.h"
#include "character_class.h"

void DisplaySoldiers(Character *pMyCreatedArmy, int numberOfSoldiers)
{
	for (int i = 0; i < numberOfSoldiers; i++){
		std::cout<<"Soldier " << i << "\t" <<  " has \t " << pMyCreatedArmy[i].health << std::endl;
	}
	system("Pause");
}


I know this code isn't great in depth or advanced at all but point being - it's fixed and here is my solution

Thanks as well firedraco you pointed out a few things that helped me with this - Cheers mate!
Topic archived. No new replies allowed.