I've managed to use classes in separate files before, but I can't seem to get it to work here. I was just programming some very basic classes for a simple game I wanted to make. Here is the main.cpp file:
#include <iostream>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
using namespace std;
int main(){
energySystem energySystem;
healthSystem healthSystem;
intelligenceSystem intelligenceSystem;
energySystem.setEnergyLevel(750);
healthSystem.setHealthLevel(100);
intelligenceSystem.setIntelligenceLevel(0);
cout << energySystem.getEnergyLevel() << endl;
cout << healthSystem.getHealthLevel() << endl;
cout << intelligenceSystem.getIntelligenceLevel() << endl;
energySystem.decreaseEnergy(100);
cout << energySystem.getEnergyLevel() << endl;
return 0;
}
Here is one of the class headers. All 3 of the classes are identical at the moment apart from the name, so I'll just post one.
energySystem.h file:
#ifndef ENERGYSYSTEM_H
#define ENERGYSYSTEM_H
class energySystem{
private:
int energyLevel;
public:
energySystem();
int getEnergyLevel();
void setEnergyLevel(int x);
void decreaseEnergy(int x);
};
#endif // ENERGYSYSTEM_H
And energySystem.cpp (this is probably the file causing the error) :
#include <iostream>
#include "energySystem.h"
#include "healthSystem.h"
#include "intelligenceSystem.h"
using namespace std;
int energyLevel;
energySystem::energySystem(){
}
int getEnergyLevel(){
return energyLevel;
}
void setEnergyLevel(int x){
energyLevel = x;
}
void decreaseEnergy(int x){
if((energyLevel - x) >= 0){
energyLevel -= x;
}
}
Here are the errors I get:
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|11|undefined reference to `energySystem::setEnergyLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|12|undefined reference to `healthSystem::setHealthLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|13|undefined reference to `intelligenceSystem::setIntelligenceLevel(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|14|undefined reference to `energySystem::getEnergyLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|15|undefined reference to `healthSystem::getHealthLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|16|undefined reference to `intelligenceSystem::getIntelligenceLevel()'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|17|undefined reference to `energySystem::decreaseEnergy(int)'|
C:\Program Files (x86)\CodeBlocks\programs\Text based game\main.cpp|18|undefined reference to `energySystem::getEnergyLevel()'|
Thanks for any help.