unresolved external symbol

This is a discussion on unresolved external symbol within the C++ Programming forums, part of the General Programming Boards category; Hmm i hate these linking errors, the error messages are next to useless. Or at least seem it. Here's whats ...

  1. #1
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72

    unresolved external symbol

    Hmm i hate these linking errors, the error messages are next to useless. Or at least seem it. Here's whats up, all im doing is making up some classes to show use of inheritance. It compiles ok, then I get this error at link time:
    CCharacter.obj : error LNK2001: unresolved external symbol "public: __thiscall CPlayer::CPlayer(void)" (??0CPlayer@@QAE@XZ)
    Debug/CCharacter.exe : fatal error LNK1120: 1 unresolved externals

    the relevant code is:

    Code:
    class CPlayer : public CCharacter
    {
    public:
    
    CPlayer();
    
    void Move() {cout << m_sName << " moves" << endl;}
    
    };
    
    int main()
    {
    CPlayer play1;
    
    play1.Attack();
    play1.Use();
    play1.Move();
    
    return 0;
    }

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    because you declared a default constructor and created an object, but never defined the constructor.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Those errors esually involve when the compiler found a
    declareation of a function but not its definition, in other words
    it cant find the function itself.
    In this case, i can find the constructor, add this:

    Code:
    CPlayer::CPlayer()
    {
    }
    under the class

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    Originally posted by Polymorphic OOP
    because you declared a default constructor and created an object, but never defined the constructor.
    No fair!! i post a nice explanation and you get to be first

  5. #5
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    thankyou, i added that, but now i get the error:
    CCharacter.cpp(55) : error C2512: 'CCharacter' : no appropriate default constructor available

    I keep getting this error, why is it? It appears to go if I take everytihng out of the constructor.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Post your code for CCharacter as well as line 55 where you are creating the object.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    my guess would be that it wants you to provide a CCharacter default constructor (no parameters).

  8. #8
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    Code:
    class CCharacter : public CWorld
    {
    public:
    string m_sName, m_sGender;
    int m_iHealth, m_iEnergy;
    
    CCharacter(string name, string gender) : m_sName(name), m_sGender(gender), m_iHealth(100), m_iEnergy(100) {}
    ~CCharacter() {}
    
    void Attack() {cout << m_sName << " attacks" << endl;}
    void Use() {cout << m_sName << " uses a weapon" << endl;}
    };
    
    class CPlayer : public CCharacter
    {
    public:
    
    CPlayer();
    
    void Move() {cout << m_sName << " moves" << endl;}
    
    };
    
    CPlayer::CPlayer()
    {                                                      // This is line 54
    }

  9. #9
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    so i could do a default constructor with nothing, then add a copy constructor thingy like CCharacter::CCharacter( /* with all my stuff in*/)?

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    As soon as you make a constructor it get's rid of the "default default" constructor -- so you have to either make one with default arguments, one with no arguments, or always pass data when you create instances (which is not good because you can't do this with arrays of objects).

  11. #11
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    still aint working... you see i remember the days when i used to enjoy programming... when i used to program in C :P

  12. #12
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Nope, that should fix it:

    Code:
    class CCharacter : public CWorld
    {
    public:
    string m_sName, m_sGender;
    int m_iHealth, m_iEnergy;
    
    CCharacter() {}
    CCharacter(string name, string gender) : m_sName(name), m_sGender(gender), m_iHealth(100), m_iEnergy(100) {}
    ~CCharacter() {}
    
    void Attack() {cout << m_sName << " attacks" << endl;}
    void Use() {cout << m_sName << " uses a weapon" << endl;}
    };

  13. #13
    Registered User gazsux's Avatar
    Join Date
    Mar 2002
    Posts
    72
    thankyou, it works. And don't worry guys, tomorrow's the hand in day for the coursework, then i get 3 weeks off in which i plan to essentially re-learn c++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21