functor作为C ++中的基类

时间:2012-10-26 23:44:14

标签: c++ functor

我有一个仿函数基类和一个类似于此的函子派生类:

class ReadSensor
{
public:
    ReadSensor();
    virtual ~ReadSensor(void){}
    virtual int operator()(void) = 0;
};

class ReadSensorDummy : public ReadSensor
{
public:
    ReadSensorDummy() : x(0) {}
    ReadSensorDummy(int x): x(x) {}
    ~ReadSensorDummy(void) {}
    int operator() (void) { return x;}
private:
    int x;
};

我正在创建它:

ReadSensor *rs =  new ReadSensorDummy(5);

它编译,但我收到以下链接错误:

Error   2   error LNK2019: unresolved external symbol "public: __cdecl ReadSensor::ReadSensor(void)" (??0ReadSensor@@QEAA@XZ) referenced in function "public: __cdecl ReadSensorDummy::ReadSensorDummy(int)" (??0ReadSensorDummy@@QEAA@H@Z) W:\SafetySystemTest.obj

有什么问题?不应该需要dynamic_cast,但我确实尝试了它并没有帮助。

1 个答案:

答案 0 :(得分:5)

您已声明为ReadSensor定义构造函数的意图,但编译器未找到任何源文件中定义的构造函数。您可以尝试定义一个空的,或删除声明。