I am using Code::Blocks, and I understand how to use classes (the basics at least). I have set up a class, and called it from main, but I did it all in the main.cpp.
When I click add -> class, I get two more file. Lets say I named my class Monster, I get Monster.cpp, and Monster.h(this one is under a new folder called header). Isn't the point of these new files to be able to put the class in there, and then call it form main.cpp? No matter what I try, I cannot get it to run, even though when I have a new project, and just put the class into the main.cpp, it works fine.
Because Monster::Monster is the constructor. You never defined the constructor in the .h file. Also, you're trying to redefine the Monster class, this is a no no.
#ifndef MONSTER_H
#define MONSTER_H
#include <string>
class Monster
{
public:
// This is a default constructor
Monster();void setHealth(int x){ health = x; }
int getHealth() { return health; }
void setWeight (float x) { weight = x; }
float getWeight () {return weight;}
// void setName (string x) {name = x; }
// string getName () {return name;}
private:
int health;
float weight;
// string name;
};
#endif // MONSTER_H
Adding that one line in will all your code to work...sort of...
In your monster.cpp file, where you actually define the Monster constructor, you try to create a new class. This class has already been declared and defined. You should get an error along the lines of trying to redefine the Monster class.
You're doing some weird things. I'm not surprised this doesn't compile. Creating a class goes as follows:
1 2 3 4
class ID
{
/** Body goes here... **/
}/** Possible object declaration **/ ;
You're trying to make two classes with the same name, in two files. Your header file should be the only place where this class is declared. Your .cpp file just defines the class.
1 2 3
//Sample .cpp file
returnType Class::methodName(type param) { /** Perform actions **/ }
/** Other methods and what not I don't feel like typing because I'm using the editor on this site **/
As you can see, the .cpp does no declaring of anything. Just simple defines what you have declared elsewhere. Or what you can do is just declare and define all in one .cpp file.
Thank you. I got it to work by deleting the Monster.cpp file, and everything seemed to work fine. Is it necessary to have that, or can I just put the body of the class into Monster.h?
Also, when I activate the string, it does not work, and gives me an error that says string (in main.cpp) does not name a type? Any suggestions for fixing this?
The difference between sticking declaration and implementation in one file as opposed to to two files is just that if you leave them in one file together, the compiler will treat all methods as inline. As far as I know, the compiler still actually decides whether or not they will be inline or not. I don't think it's any different than explicitly stating a function as inline.
I don't think the compiler treats function/class member function definitions/declarations any differently over two files or one. I believe it was for easy readability. When someone includes your header file, they might be curious as to what functions or class members you have and can easily see. It wouldn't require a bunch of extra lines to be stored at the bottom. I believe this is more so true for multiple class declarations in one file (a contact class and an address book class).
I was also told there is some issue with things all being in the same file, but I was unsure as to where the issue comes from since I was never able to find it nor replicate it. I know have made it so that my header file includes my cpp so I no longer have to add more files to my projects. IMHO, it makes it simpler and does the same thing.