How to call a class in Code::Blocks

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.
Did you make sure your it is Monster.cpp and not Monster.c?


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13

#include <iostream>
#include "foo.h"

using namespace std;

int main()
{
    foo f;
    cout << f.ifoo << endl;
    return 0;
}



foo.h
1
2
3
4
5
6
7
8
9
10
11
12
13

#ifndef __FOO_H__
#define __FOO_H__

class foo
{
    public:
        int ifoo;
        foo();
};


#endif /*__FOO_H__*/ 



foo.cpp
1
2
3
4
5
6
7
8

#include "foo.h"

foo::foo()
{
    ifoo = -1;
}


Last edited on
That works, but I cannot understand why mine won't. Here is mine.
I commented out the string b/c I thought that might be my problem but it wasn't.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "Monster.h"

using namespace std;

int main()
{
    Monster reptar;
    reptar.setHealth(100);
    reptar.setWeight(185);
//    reptar.setName("Reptar");
    cout << "<------->" << endl;
    cout << reptar.getHealth() << endl << "  " << reptar.getWeight() << endl;
    cout << "<------->";
    return 0;
}


monster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef MONSTER_H
#define MONSTER_H
#include <string>


class Monster
{
    public:
        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


monster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "Monster.h"

Monster::Monster()
{
class Monster
{
    public:
        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;
};


}


It flags it at the Monster::Monster() in monster.cpp
Last edited on
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.
How do I define a constructer in the .h file?

And what do you mean I am trying to redefine the monster class?
monster.h
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
#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.

If you separate them, then they act normally.
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.
They are treated as inline if in one file. It's a well known fact.
Topic archived. No new replies allowed.