Need Help With Linking Files For A Class

Im not sure if this question belongs in beginner section but i need help with how to link my classes to the rest of my code? I would put my code up but it is seperated into several files. I will use example:



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

//i want to move this class to a global file so that any of my files can create 
//objects using the class and change the object things.
class Enemy{
public:
int health;
int damage;
};

int main(){

}


I was thinking something along the lines of this:
classFile.h:
1
2
3
4
#ifndef CLASSFILE
#define CLASSFILE
class Enemy;
#endif 

classFile.cpp:
1
2
3
4
5
6
7
8
#include <iostream>
#include "classFile.h"
using namespace std;
class Enemy{
public:
int health;
int damage;
};

main.cpp:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "classFile.h"

using namespace std;

main(){
Enemy Ghoul;
Ghoul.health = 50;
Ghoul.damage = 10;
}


Would this work? thanks =)
closed account (28poGNh0)
classFile.h:
1
2
3
4
5
6
7
8
9
10
11
# ifndef CLASSFILE
# define CLASSFILE

class Enemy
{
public:
    int health;
    int damage;
};

# endif 


classFile.cpp:
1
2
3
# include "classFile.h"

// implementations of this class (constructor,member functions,...) 

main.cpp:
1
2
3
4
5
6
# include "classFile.h"

int main()
{
     // here you can call you class
}
Ok so what if i wanted to run a function calling the classes and then use them in the main?

main(){
createObjects();
cout << zombie.health << endl;
}

createObjects:
int createObjects(){
Enemy zombie;
zombie.health = 10;
zombie.damage = 2;
return 0;
}
closed account (28poGNh0)
I dont understand what are you trying to do
Topic archived. No new replies allowed.