未解决的外部[构造函数]

时间:2012-10-29 15:57:53

标签: c++ constructor linker externals

  

可能重复:
  What is an undefined reference/unresolved external symbol error and how do I fix it?

我的链接器有问题,我无法解决.. 我已经尝试了任何我能想到的东西 我有一个Baseclass(Person)和一个派生类(Dealer),我只想从CardDeack类中调用Constructor,它是Dealer类中的一个成员。

这是我的代码:

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;
};

我老实说试过了我能想到的但我无法弄清楚错误是什么......

这是编译Dealer.cpp时的输出:

1&gt; Dealer.obj:错误LNK2019:未解析的外部符号“public:virtual __thiscall Person ::〜Person(void)”(?? 1Person @@ UAE @ XZ)在函数__unwindfunclet中引用$ ?? 0Dealer @@ QAE 3 H @ Z $ 0

1&gt; Dealer.obj:错误LNK2001:未解析的外部符号“public:virtual bool __thiscall Person :: TakeCard(class card)”(?TakeCard @ Person @@ UAE_NVCard @@@ Z)

1&gt; Dealer.obj:错误LNK2001:未解析的外部符号“public:virtual bool __thiscall Person :: Lost(void)”(?Lost @ Person @@ UAE_NXZ)

1&gt; Dealer.obj:错误LNK2001:未解析的外部符号“protected:virtual void __thiscall Person :: CheckLost(void)”(?CheckLost @ Person @@ MAEXXZ)

1 个答案:

答案 0 :(得分:-2)

看起来你正试图将Dealer.cpp编译成一个程序。这不起作用,因为它取决于Person中方法的定义,可能在Person.cpp中。如果您向我们展示了您用于编译的命令,那将会很有帮助。但假设您正在使用g ++,您可能尝试做的是

g++ Dealer.cpp

你应该做的是

g++ Person.cpp Dealer.cpp etc.

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

等,然后

g++ Dealer.o Person.o etc.
相关问题