Hello DaniWeb, its been a long time! Anyway, I'm trying to put classes and functions inside .h files and define them in respective .cpp files. Whenever I try this, I get a weird error that I think has something to do with my constructor:

obj\Debug\main.o||In function `_static_initialization_and_destruction_0': |
main.cpp|33|undefined reference to `Character::Character()'|

I am on 64bit Windows 7, using CodeBlocks IDE.

Here is my "character.h"

#ifndef CHARACTER_H
#define CHARACTER_H
#include <iostream>
#include <string>
#include <vector>



class Character{
    public:
        Character(); // constructor
        int health;
        int magic;
        int level;
        int eyeColor;
        std::vector<std::string> inventory;
        std::vector<std::string> abilities;
};

#endif

Here is my "character.cpp"

#include "character.h"

Character::Character(){
    inventory.push_back("SWORD");
    inventory.push_back("SHIELD");
    abilities.push_back("ATTACK");
    eyeColor = 1;
    health = 100;
    magic = 50;
    level = 1;
}

And here is my main.cpp

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

int main(){
   Character mainCharacter;
   std::cout << mainCharacter.health;
   return 0;
}

Thanks!

Edited 4 Years Ago by Hawkpath: n/a

This compiles for me:

I left the class functions (.cpp) file alone and the main but changed the class to:

#ifndef CHARACTER_H
#define CHARACTER_H

#include <iostream>
#include <string>
#include <vector>
class Character
{
       public:
        Character();
    
   private:
    
    int health;
    int magic;
    int level;
    int eyeColor;
    std::vector<std::string> inventory;
    std::vector<std::string> abilities;
    
};

#endif

Hope this helps :)

Edited 4 Years Ago by phorce: n/a

Hey phorce, thanks for your fast response. I changed the .h file the same way you did, making the constructor public and everything else private but I still get the same error that I'm making an undefined reference to "Character::Character" which I'm assuming means that it doesn't recognize the constructor.

Well, what is on line 33 of main.cpp?

On line 33 is the creation of the Character Object - "Character mainCharacter;"

Edited 4 Years Ago by Hawkpath: n/a

In code::blocks if you don't mark your files in your project to be globally included, the compilation later won't be successful. In that case you have to include them manually. Did you try to include "character.cpp" in your main function?

I am just guessing here, but I had troubles with it before.

Edited 4 Years Ago by LRRR: n/a

In code::blocks if you don't mark your files in your project to be globally included, the compilation later won't be successful. In that case you have to include them manually. Did you try to include "character.cpp" in your main function?

I am just guessing here, but I had troubles with it before.

Your suggestion worked! Thanks a lot to everyone who took the time to help out a nooby :)

This question has already been answered. Start a new discussion instead.