Sign in to follow this  
Hylas

Including multiple files

Recommended Posts

Hylas    124
Hello, I have been programming for quite a while now and my projects are starting to get big enough that it seems like it is time to break them up into separate files. The problem is I can seem to get this to work. I have a rather large class Player that I broke up into a Player.h with all my class definition. Then I put the Class implementation in Player.cpp. Here is what I have tried and I get alot of errors mostly cout errors. Player.h

#include <string>

using namespace std; 

class Player 

{

public:  
// CLASS MEMBER DATA:
int Level; 
int EXP; 
int ExpTNL; 

bool DoubleAttack; 
bool TrippleAttack; 

int ClassID;        // 1: Warrior 2: Paladin 3: Ranger 3: Red Mage 5: Black Mage 6: Ninja 7: Monk
string ClassName; 	
	
int Health; 
int Magic; 
int Attack;   //Passive Attributes
int Defense; 
int Evasion; 
int Speed; 
int Accuracy; 

int Strength;
int Agility; 
int Dexterity;    //Active Attributes
int Intelligence; 
int Vitality; 

// Class Methods
Player(); 

void CalculateDefense(); 
void CalculateOffense(); 
void CalculatePassive(); 
int CalculateExpTNL(); 

void CombineCalculations(); 

void SetClassName(); 
void GetClass(); 
int  GetClassID(); 

void SetStatus(int STR, int AGI, int DEX, int INT, int VIT); 
void PrintStatus(); 

};


Player.cpp

#include "Player.h"

// Initialize The Object

Player::Player() {

Level = 0; 
EXP = 0; 
ExpTNL = 0; 
ClassName = ""; 
ClassID = 0; 

DoubleAttack = false; 
TrippleAttack = false; 

Strength = 0;
Agility = 0; 
Dexterity = 0;   
Intelligence = 0; 
Vitality = 0;

Health = 0; 
Magic = 0; 
Attack = 0;   
Defense = 0; 
Evasion = 0; 
Speed = 0;
Accuracy = 0; 

}

// Lets Do Some Calculations      
// I will use 15 as a base stat to get an idea of what the results will look like

void Player::CalculateDefense() {

	switch(GetClassID()) {
	
	case 1: // Warrior

		Defense = (Vitality * 2.5) + 10;  // 47
		Health = (Vitality * 7) + 80;     // 185
		break; 
	case 2: // Paladin

		Defense = (Vitality * 4) + 20;  // 80
		Health = (Vitality * 8) + 120;   // 240
		break; 
	case 3: // Ranger

		Defense = (Vitality * 2) + 10;   // 40
		Health = (Vitality * 5.8) + 50;  // 137
		break; 
	case 4: // Red Mage

		Defense = (Vitality * 1.8) + 8;  // 35
		Health = (Vitality * 5) + 50;    // 125
		break; 
	case 5: // Black Mage

		Defense = (Vitality * 1.5) + 5;  // 32
		Health = (Vitality * 4.3) + 40;  // 104
		break; 
	case 6: // Ninja

		Defense = (Vitality * 2) + 10;   // 40
		Health = (Vitality * 6) + 60;    // 150
		break; 
	case 7: // Monk

		Defense = (Vitality * 2) + 10;    // 40
		Health = (Vitality * 8.5) + 150;  // 277
		break; 
	}
}

void Player::CalculateOffense() {

	switch(GetClassID()) {

	case 1:  // Warrior

		Attack = (Strength * 12) + 20; 
		Accuracy = (Dexterity * 3) + 20; 
		break; 
	
	case 2:  // Paladin
		Attack = (Strength * 8) + 20; 
		Accuracy = (Dexterity * 3) + 20;
		break; 
	
	case 3:  // Ranger
		Attack = ((Strength * 4) + (Agility * 7)) + 20; 
		Accuracy = (Dexterity * 3) + 20;
		break; 
	
	case 4:  // Red Mage
		Attack = ((Strength * 2) + (Intelligence * 7)) + 20; 
		Accuracy = (Dexterity * 3) + 20;
		break; 
	
	case 5:  // Black Mage
		Attack = (Intelligence * 15);
		Accuracy = (Dexterity * 3) + 20;
		break; 
	
	case 6:  // Ninja
		Attack = ((Strength * 4) + (Dexterity * 5)) + 20; 
		Accuracy = (Dexterity * 3) + 20;
		break; 
	
	case 7:  // Monk
		Attack = (Strength * 7) + 20; 
		Accuracy = (Dexterity * 3) + 20;
		break; 
	}
}


void Player::CalculatePassive() {

	switch(GetClassID()) {

	case 1:  // Warrior
		Magic = (Intelligence * 5) + 50; 
		Evasion = (Agility * 2) + 10; 
		Speed = 1; 
		break; 
	
	case 2:  // Paladin
		Magic = (Intelligence * 5.5) + 60; 
		Evasion = (Agility * 2) + 10; 
		Speed = 1;
		break; 
	
	case 3:  // Ranger
		Magic = (Intelligence * 5) + 50; 
		Evasion = (Agility * 2.5) + 12; 
		Speed = 1;
		break; 
	
	case 4:  // Red Mage
		Magic = (Intelligence * 7) + 60; 
		Evasion = (Agility * 1.3) + 10; 
		Speed = 1;
		break; 
	
	case 5:  // Black Mage
		Magic = (Intelligence * 8) + 75; 
		Evasion = (Agility * 1.3) + 10; 
		Speed = 1;
		break; 
	
	case 6:  // Ninja
		Magic = (Intelligence * 5) + 50; 
		Evasion = (Agility * 6) + 15; 
		Speed = 1;
		break; 
	
	case 7:  // Monk
		Magic = (Intelligence * 5) + 50; 
		Evasion = (Agility * 2.5) + 10; 
		Speed = 2;
		break; 
	}
}

int Player::CalculateExpTNL() {

	int AmountTNL[10] = {240, 280, 315, 375, 420, 485, 550, 630, 700, 800}; 

	return AmountTNL[Level]; 
}

void Player::CombineCalculations() {

	CalculateDefense(); 
	CalculateOffense(); 
	CalculatePassive(); 

}

void Player::SetClassName() {

	switch(GetClassID()) {
	
	case 1: 
		ClassName = "Warrior"; 
	
	case 2: 
		ClassName = "Paladin"; 
	
	case 3: 
		ClassName = "Ranger"; 
	
	case 4: 
		ClassName = "Red Mage"; 
	
	case 5: 
		ClassName = "Black Mage"; 
	
	case 6: 
		ClassName = "Ninja"; 
	
	case 7: 
		ClassName = "Monk";

	}
}

void Player::GetClass() {
	
	int Choice; 
	
	cout << "Choose your class: "; 
	cout << "\n"; 
	cout << "1: Warrior"; 
	cout << "\n"; 
	cout << "2: Paladin"; 
	cout << "\n"; 
	cout << "3: Ranger"; 
	cout << "\n"; 
	cout << "4: Red Mage"; 
	cout << "\n"; 
	cout << "5: Black Mage"; 
	cout << "\n"; 
	cout << "6: Ninja"; 
	cout << "\n"; 
	cout << "7: Monk"; 
	cout << "\n"; 
	cin >> Choice; 

	ClassID = Choice; 

}

int Player::GetClassID() {

	return ClassID; 

}

void Player::SetStatus(int STR, int AGI, int DEX, int INT, int VIT) {
	
	Strength = STR; 
	Agility = AGI; 
	Dexterity = DEX; 
	Intelligence = INT; 
	Vitality = VIT; 

}

void Player::PrintStatus() {

	cout << "Current Status "; 
	cout << "\n\n"; 
	cout << "HP " << "( " << Health << " )"; 
	cout << "\n"; 
	cout << "MP " << "( " << Magic << " )"; 
	cout << "\n\n"; 
	
	cout << "STR: " << Strength; 
	cout << "\n"; 
	cout << "AGI: " << Agility; 
	cout << "\n"; 
	cout << "DEX: " << Dexterity; 
	cout << "\n";
	cout << "INT: " << Intelligence; 
	cout << "\n"; 
	cout << "VIT: " << Vitality; 
	cout << "\n\n";

	cout << "Attack " << Attack << "    " << "Evasion " << Evasion << "    " << "Defense " << Defense; 
	cout << "\n"; 

}


Main.cpp

#include <iostream>
#include "Player.cpp"


int main() 

{

	return 0; 
 
}


Please help me gamedev.net. You're my only hope...

Share this post


Link to post
Share on other sites
_cdcel    114
Including multiple files can be difficult.
First of all, if your "Player.cpp" is included in your project, then it will automatically be compiled; you do not need to include it. (Change it to "player.h".)
Each .cpp file inside your project will compile its own code. Therefore, when the compiler compiles your program, it may start at Player.cpp. Player.cpp will include player.h*, which tells the compiler that class "player" exists. The compiler will then go through the player.cpp file and provide definitions for the class and etc. When it reaches the end of the file, it then outputs the compilation into an .obj file to provide to the linker later. The compiler then moves to the main.cpp file. The main.cpp file includes Player.cpp, which includes player.h, and creates code for the player class. After all the .cpp files are compiled, it steps into the linker. The linker, however, will complain that there are multiple definitions of class "player" members, since main.obj and player.obj are providing the linker with its own player class definitions.

In short, your goal with including multiple files is to not provide multiple definitions (albeit the same definitions).

Keep declarations in your header file, and keep definitions in cpp files.
If you want your main.cpp file to use the player class, include "Player.h" because main.cpp just needs to know of its existence.

Here are some links that might help:
http://en.wikipedia.org/wiki/Declaration_%28computer_science%29
http://en.wikipedia.org/wiki/Function_prototype

I also recommend using precompiled headers:
http://en.wikipedia.org/wiki/Precompiled_headers

*(and player.cpp will not know that you included iostream in main.cpp)

Share this post


Link to post
Share on other sites
nobodynews    3126
You include header files, not source files. There's a simple reason not to do it: the compiler will compile the .cpp file that is included, then compile it again in the files that include it producing multiple definition errors. Simple fix: Change #include "player.cpp" to #include "player.h". Don't forget to surround the code in player.h wide include guards.

Your main function doesn't do anything. So even when you DO get your program to compile it won't do anything as you never called any functions in main.

The reason you are getting errors about cout is because you never include iostream.

You should never put 'using namespace std;' in a header file as you now force everyone who uses your header file to pollute the global namespace. If you feel you must use the 'using namespace' to save a few moments of typing then keep it in a source file.

Read this<a/> until you understand it. Then read it again for extra measure in case you were lying to yourself.

Hope that helps.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this