仅具有纯虚函数的类中的构造函数会导致派生类中的错误

时间:2015-03-06 01:56:19

标签: c++ inheritance

我有一个抽象类IPrinter,它只包含纯虚函数。

using namespace std;
class IPrinter
{
public:
    virtual void Print() =0;
    virtual void Print(string) = 0;
    virtual void SetStreamData(string) =0;
private:
    IStreamBehavior* streamBehavior;
    string _name;
};

一个实现它的类

class HtmlPrinter :
    public IPrinter
{
public:
    HtmlPrinter();
    HtmlPrinter(IPrinter*);
    ~HtmlPrinter();
    virtual void Print();
    virtual void Print(string);
    vector<string> GetPrintList();
    virtual void SetStreamData(string);
private:
    IPrinter* printer;
    string streamData;
    void Paragraph();
};

CPP

HtmlPrinter::HtmlPrinter()
{
}
//error on this line
HtmlPrinter::HtmlPrinter(IPrinter* printer) :printer(printer){}

void HtmlPrinter::Print()
{
    this->printer->Print();
}
void HtmlPrinter::Print(string s)
{

}
vector<string> HtmlPrinter::GetPrintList()
{
    vector<string> myStrings;
    return myStrings;
}
void HtmlPrinter::SetStreamData(string streamData)
{
    this->streamData = streamData;
}
HtmlPrinter::~HtmlPrinter()
{
}
void HtmlPrinter::Paragraph()
{
    string old = streamData;
    streamData += "<p>" + old += "</p>";
}

当我向IPrinter添加构造函数或析构函数时,HtmlPrinter的构造函数中出现IPrinter*的错误。为什么我不能向IPrinter添加构造函数/析构函数?

错误讯息:

Error   2   error LNK2019: unresolved external symbol "public: __thiscall 

    IPrinter::IPrinter(void)" (??0IPrinter@@QAE@XZ) referenced in function "public: __thiscall HtmlPrinter::HtmlPrinter(class IPrinter *)" (??0HtmlPrinter@@QAE@PAVIPrinter@@@Z)    C:\Users\darrin\documents\visual studio 2013\Projects\ShapeStore\ShapeStore\HtmlPrinter.obj ShapeStore

1 个答案:

答案 0 :(得分:0)

我在你的代码中没有发现任何错误。检查一次

<script src="http://ideone.com/e.js/1HWcfn" type="text/javascript" ></script>

http://ideone.com/1HWcfn