【问题标题】:Link error missing vtable链接错误缺少 vtable
【发布时间】:2015-01-18 08:46:30
【问题描述】:

我正在定义一个“函数”类和另外两个从“函数”继承的“多项式”和“仿射”类。

    class function {

    public:
        function(){};
        virtual function* clone()const=0;
        virtual float operator()(float x)const=0; //gives the image of a number by the function
        virtual function* derivative()const=0;
        virtual float inverse(float y)const=0; 
        virtual ~function(){}

    };

    class polynomial : public function {
    protected:
        int degree;
    private:
        float *coefficient;
    public:

        polynomial(int d);
        virtual~polynomial();
        virtual function* clone()const;
        int get_degree()const;
        float operator[](int i)const; //reads coefficient number i
        float& operator[](int i); //updates coefficient number i
        virtual float operator()(float x)const; 
        virtual function* derivative()const;
        virtual float inverse(float y)const; 

    };

    class affine : public polynomial {
        int a; 
        int b; 
        //ax+b
public:
        affine(int d,float a_, float b_);
        function* clone()const;
        float operator()(float x)const;
        function* derivative()const;
        float inverse(float y)const;
        ~affine(){}
    };

我实现了与“多项式”相关的所有方法,并希望对其进行测试。对于派生方法,我在实例度数等于0的情况下使用仿射构造函数。所以我必须在运行测试之前定义这个构造函数。

polynomial::polynomial(int d)
{
    assert(d>=0);
    degree=d;
    coefficient=new float [d+1];
}

polynomial::~polynomial()
{
    delete[] coefficient;
}
function* polynomial::derivative()const
{
    if(degree==0)
    {
        return new affine(0,0,0);
    }
    polynomial* deriv=new polynomial(degree-1);
    for(int i=0;i<degree;i++)
        deriv[i]=(i+1)*coefficient[i+1];   
    return deriv;
}

affine::affine(int d,float a_, float b_):polynomial(d)
{ 
    assert(d==0 || d==1);
    degree=d;
    a=a_;
    b=b_;
}

我的第一个测试是:

#include "function.h"

int main(int argc, const char * argv[])
{
    //p=x^3
    polynomial p(3); 
    for(int i=0;i<=2;i++) p[i]=0;
    p[3]=1;
    cout<<"21^(1/3)="<<p.inverse(21);



    return 0;
}

当我运行它时,我得到一个链接错误:

Undefined symbols for architecture x86_64:
  "vtable for affine", referenced from:
      affine::affine(int, float, float) in polynomial.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我不知道为什么。

【问题讨论】:

  • 你确定你有一个virtual ~polynomial()定义吗? (而不仅仅是一个声明)。
  • 问题的执行示例将有助于回答问题。
  • 你在子类中实现了逆向吗?
  • 听起来您没有定义在affine 中声明的所有虚函数(如错误消息中的注释所述)。具体来说,您是否定义了第一个 affine::clone
  • 我已经像这样实现了~polynomial():polynomial::~polynomial() { delete[] coefficient;在“逆”方面,我只是为“多项式”实现了它。 “仿射”中没有虚拟方法,但我没有定义“仿射”中的所有方法,因为我想先测试“多项式”及其方法,然后再进行其他操作

标签: c++ xcode4 vtable


【解决方案1】:

阅读错误信息:

注意:缺少 vtable 通常意味着第一个非内联虚拟成员函数没有定义。

您似乎还没有实现第一个非内联虚拟成员函数。查看类定义,即afine::clone

您只描述了实现构造函数。您还必须实现所有非纯虚函数。

【讨论】:

  • 是否应该实现所有“仿射”方法,即使我没有在主文件中全部使用它们(只是测试我目前编写的一些方法)?
  • @dada:你必须定义你声明的每一个非纯虚函数。由于它们都覆盖了polynomial 中定义的函数,因此您可以删除/注释掉这些声明,直到您需要它们为止;或者你可以写一个最小的定义,例如返回一个虚拟值,或调用基类版本。
【解决方案2】:

正如你们所说,这是关于“仿射”的未定义覆盖方法。

【讨论】:

    猜你喜欢
    • 2013-06-30
    • 2012-08-11
    • 2013-02-22
    • 2014-10-12
    • 1970-01-01
    • 2016-06-04
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多