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

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

问题描述

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

这里是抽象基类:

班级记录{民众:虚拟 int getID()=0;虚拟记录 *clone();};

和实现:

class sdata:公共记录{民众:sdata(std::string s = ""){data=s;ID=atoi(data.substr(0,8).c_str());}虚拟 int getID(){返回 ID;}私人的:std::string 数据;整数标识;};

抱歉,这里是完整的错误信息:

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

也许是这段代码导致了错误:

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

解决方案

如果不查看导致错误的代码,就很难确切知道发生了什么.如果这是一个编译时错误,我在这里看不到任何会导致它的东西.

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

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

(2) 派生类调用基类版本的函数但未实现.

显示这两个错误的示例如下:<代码><预>结构基{根据(){call_foo();//哎呀,间接调用了 Base::foo()(场景一)}void call_foo() const {富();}受保护:虚拟无效 foo() const = 0;};

结构派生:基{受保护:虚拟无效 foo() const {基地:: foo();//糟糕,未实现的虚拟基函数(场景 2)}};

int main() {派生().call_foo();}

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

我在你的示例代码中观察到记录有一个非纯虚拟的 clone() 成员函数返回一个 record *.由于记录是抽象的,您不能直接创建记录(只有它的具体子类).这表明您的 clone() 成员函数可能也应该是纯虚拟的;如果它试图(例如)return new record(),你会得到一个错误,你的基类有纯虚函数.

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?

here is the abstract base class:

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

and the implementation:

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:

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) Calling the member function from within the base class's constructor or destructor (even indirectly).

(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 Derived : Base { protected: virtual void foo() const { Base::foo(); // Oops, unimplemented virtual base function (Scenario 2) } };

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

== UPDATE: Possible compile-time error ==

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天全站免登陆