So, I'm working on defining a class, it's functions, and using them in a program. I want to define the class in a header file, the functions in a source file, and the program itself in another source file.
The program thus far is very short, and so it can all fit into one file, but I'm trying to practice intermediate practices. There's a small problem, however.
If all the code is in one file, it compiles and runs without a hitch.
If I break it into the three files, header and two source, it won't compile.
The error it gives me is:
"In file included..." (The header file, class definition)
"'string' does not name a type'"
"'string has not been declared'"
Below is my code.
creature.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
class Creature {
int creatureHP;
int creatureAC;
int creatureATK;
string creatureName;
public:
void setStats(int, int, int, string);
void setDamage(int);
void displayStats();
string getName();
int isDead();
};
|
creature.cpp
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
|
#include "creature.h"
void Creature::setStats(int a, int b, int c, string d) {
creatureHP = a;
creatureAC = b;
creatureATK = c;
creatureName = d;
}
void Creature::setDamage(int a) {
creatureHP = a;
}
void Creature::displayStats() {
cout << "There is now a " << creatureName << "." << endl;
cout << "The creature has the following stats:" << endl;
cout << creatureHP << " hit points." << endl;
cout << creatureAC << " armour class." << endl;
cout << creatureATK << " base attack." << endl;
}
string Creature::getName() {
return creatureName;
}
int Creature::isDead() {
return creatureHP;
}
|
creator.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include "creature.h"
using namespace std;
int main() {
Creature theif;
theif.setStats(10, 10, 10, "Theif");
theif.displayStats();
int x = 0;
cin >> x;
return 0;
}
|