0

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 by Hawkpath: n/a

4
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by Hawkpath
Featured Replies
  • 1

    This compiles for me: I left the class functions (.cpp) file alone and the main but changed the class to: [code] #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; }; … Read More

  • 1

    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 … Read More

1

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 by phorce: n/a

0

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 by Hawkpath: n/a

1

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 by LRRR: n/a

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.