未实现的纯虚拟方法? [英] Unimplemented Pure Virtual Method?

查看:356
本文介绍了未实现的纯虚拟方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题所在:尝试编译时,我不断收到未实现的纯虚方法错误。我已经在抽象基类中实现了所有纯虚方法。有什么想法吗?

Here is the problem: I keep getting the unimplemented pure virtual method error when trying to compile. I have implemented all of the pure virtual methods in the abstract base class. Any ideas?

这是抽象基类:

class record{
public:
    virtual int getID()=0;
    virtual record *clone(); 
};

并执行:

class sdata: public record{
public:
    sdata(std::string s = ""){data=s; ID=atoi(data.substr(0,8).c_str());}
    virtual int getID(){return ID;}
private:
    std::string data;
    int ID;
};

对不起,这是完整的错误消息:

sorry, here is the complete error message:

'record'中未实现的纯虚拟方法'getID'

Unimplemented pure virtual method 'getID' in 'record'

也许这段代码导致了错误,然后:

Perhaps this bit of code is causing the error then:

int hashTable::hash(record *x) {
   return floor(m * (x->getID() * A - floor(x->getID() * A)));
}


推荐答案

没有看到导致错误,很难确切知道发生了什么。如果这是编译时错误,那么我在这里看不到任何会导致它的错误。

Without seeing the code causing the error, it's difficult to know exactly what's going on. If this is a compile-time error, I don't see anything here that would cause it.

但是,如果您看到运行时错误,则这是两个我能想到的常见原因有:

However, if you're seeing a runtime error, the two most common causes of this that I can think of are:

(1)从基类的构造函数或析构函数内调用成员函数(甚至是间接调用)。

(1) Calling the member function from within the base class's constructor or destructor (even indirectly).

(2)派生类在未实现的情况下调用函数的基类版本。

(2) The derived class calling the base class's version of the function without it being implemented.

这些错误将是:

An example showing both of these errors would be:


struct Base {
    Base()
    {
        call_foo(); // Oops, indirectly calls Base::foo() (Scenario 1)
    }
    void call_foo() const {
        foo();
    }
protected:
    virtual void foo() const = 0;
};

struct派生:基础{受保护的

virtual void foo()const {
Base :: foo(); //糟糕,未实现的虚拟基函数(场景2)
}
};

struct Derived : Base { protected: virtual void foo() const { Base::foo(); // Oops, unimplemented virtual base function (Scenario 2) } };

int main(){
Derived() .call_foo();
}

int main() { Derived().call_foo(); }

==更新:可能的编译时错误==

我在您的示例代码中观察到该记录具有非纯虚拟clone()成员函数,该函数返回记录* 。由于记录是抽象的,因此您无法直接创建记录(只能创建其具体的子类)。这表明您的clone()成员函数可能也应该是纯虚拟的。如果它试图(例如)返回新记录(),则会收到一个错误,指出您的基类具有纯虚函数。

I observe in your example code that record has a non-pure-virtual clone() member function returning a record *. Since record is abstract, you can't create a record directly (only its concrete subclasses). This suggests that your clone() member function should probably also be pure virtual; if it tries to (for example) return new record(), you will get an error that your base class has pure virtual functions.

这篇关于未实现的纯虚拟方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆