1

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I have a problem with the Linker which I just can't solve.. Already tried anything I could think of I have a Baseclass (Person) and a Derived Class (Dealer) and I just want to call the Constructor from the CardStack Class which is a member in the Dealer class.

Here is my code:

Person.h

#ifndef PERSON_H
#define PERSON_H
#include "Card.h"
#include "Hand.h"

class Person
{
public:
    Person(void);
    virtual ~Person(void);
    virtual bool TakeCard(Card c);
    virtual bool Lost(void);

protected:
    virtual void CheckLost(void);
    bool b_Lost;
    Hand m_Hand;
};
#endif

Dealer.h

    #ifndef DEALER_H
#define DEALER_H

#include "Person.h"
#include "Card.h"
#include "CardStack.h"

class Dealer : public Person
{
public:
    Dealer(int stackcount);
    virtual ~Dealer(void);
    bool TakeCard(Card c);
    bool Lost(void);
    Card GiveCard(Card c);

protected:
    void CheckLost(void);
    CardStack m_GameStack;
};
#endif

Dealer.cpp

#include "Dealer.h"

Dealer::Dealer(int stackcount) : Person(), m_GameStack(stackcount)
{

};

Dealer::~Dealer(void)
{

};

bool Dealer::TakeCard(Card c)
{
    if(!b_Lost || m_Hand.GetTotal() <= 17)
    {
        m_Hand.Take(c);
        CheckLost();
        return true;
    }

    return false;
};

void Dealer::CheckLost()
{
    if (m_Hand.GetTotal() > 21)
    {
        b_Lost = true;
    }
};

bool Dealer::Lost()
{
    return b_Lost;
};

I honestly tried everthing I could think of but I couldn't figure out what the mistake is...

Here is the Output when compiling Dealer.cpp:

1>Dealer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Person::~Person(void)" (??1Person@@UAE@XZ) referenced in function __unwindfunclet$??0Dealer@@QAE@H@Z$0

1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::TakeCard(class Card)" (?TakeCard@Person@@UAE_NVCard@@@Z)

1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::Lost(void)" (?Lost@Person@@UAE_NXZ)

1>Dealer.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Person::CheckLost(void)" (?CheckLost@Person@@MAEXXZ)

Community
  • 1
  • 1
TheSentry
  • 23
  • 1
  • 4
  • The linker is telling you exactly what is wrong. See http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574407#12574407 – Luchian Grigore Oct 29 '12 at 15:58
  • So its just the virtual destructor which is making me trouble? And sorry if its a duplicate i looked at a lot of external symbol mistakes but none matched mine :( – TheSentry Oct 29 '12 at 16:02
  • but I already implemented the virtual destructor in Dealer::Dealer() Oh got it! Declare the contructor as pure ^^ thy :) – TheSentry Oct 29 '12 at 16:04
  • The problem is `Person`, not `Dealer`. And it's not *just* the destructor, it's also the rest of the virtual methods - either provide an implementation, either declare them as pure. But, honestly, it's all in the linked answer. – Luchian Grigore Oct 29 '12 at 16:05
  • Luchian I declared now all of the virtual functions as pure but it still gives my two unresolved externals: 1>Dealer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Person::~Person(void)" (??1Person@@UAE@XZ) referenced in function __unwindfunclet$??0Dealer@@QAE@H@Z$0 1>Dealer.obj : error LNK2019: unresolved external symbol "public: __thiscall Person::Person(void)" (??0Person@@QAE@XZ) referenced in function "public: __thiscall Dealer::Dealer(int)" (??0Dealer@@QAE@H@Z) – TheSentry Oct 29 '12 at 16:14
  • Did you even read the answer I linked? First sentence, **in bold** - **"A pure virtual destructor needs an implementation."**. Also, either remove the constructor from `Person` or implement it. – Luchian Grigore Oct 29 '12 at 16:17
  • Yes I did implement it and i removed the code!!! Still nothing changed... – TheSentry Oct 29 '12 at 16:22
  • @Why did you remove the code after implementing it? – Luchian Grigore Oct 29 '12 at 16:27
  • I fixed it... I had some other issues with the linking... but thanks for the help :) & no I just removed the constructor and the destructor for the Person class – TheSentry Oct 29 '12 at 16:31

1 Answers1

-2

It looks like you are trying to compile Dealer.cpp into a program on its own. That doesn't work because it depends on the definition of the methods in Person, which are probably in Person.cpp. It would have been helpful if you had shown us the command you used to compile. But assuming you're using g++, what you probably tried to do is

g++ Dealer.cpp

What you should have done is either

g++ Person.cpp Dealer.cpp etc.

or

g++ -c Dealer.cpp
g++ -c Person.cpp

etc., and then

g++ Dealer.o Person.o etc.
amaurea
  • 4,950
  • 26
  • 35