链接错误:未定义对"vtable for XXX"的引用 [英] Linking error: undefined reference to `vtable for XXX`

查看:66
本文介绍了链接错误:未定义对"vtable for XXX"的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处存在一些链接错误.我在网上寻找了东西,但仍然找不到问题.我该如何解决?

  g ++ test.cpp -o测试/tmp/ccDfCj4N.o:在函数Interval :: Interval()中:test.cpp :(.text._ZN8IntervalC2Ev [Interval :: Interval()] + 0x9):未定义对"vtable for Interval"的引用/tmp/ccDfCj4N.o:在函数IntInterval ::〜IntInterval()中:test.cpp :(.text._ZN11IntIntervalD0Ev [IntInterval ::〜IntInterval()] + 0x1d):未定义对`Interval ::〜Interval()'的引用/tmp/ccDfCj4N.o:在函数IntInterval ::〜IntInterval()中:test.cpp :(.text._ZN11IntIntervalD1Ev [IntInterval ::〜IntInterval()] + 0x1d):未定义对`Interval ::〜Interval()'的引用/tmp/ccDfCj4N.o :(.rodata._ZTI11IntInterval [用于IntInterval的typeinfo] + 0x10):未定义对用于间隔的typeinfo"的引用collect2:ld返回1退出状态 

这是代码!对于一个试验程序,所有类都在同一个文件中.

  #include< iostream>#include< vector>#include< utility>使用命名空间std;#define MAX_IP_RANGE 4294967295类Interval {上市:虚拟间隔* interval_copy()= 0;虚拟无符号长get_begin()= 0;虚拟无符号长get_end()= 0;虚拟无符号长get_length()= 0;虚拟间隔* get_intersect(间隔*间隔)= 0;//检查两个区间是否有交集虚拟间隔* copy()= 0;虚拟〜Interval();虚拟布尔is_equal(Interval * interval){无符号长b1 = this-> get_begin();无符号长e1 = this-> get_end();无符号长b2 = interval-> get_begin();无符号长e2 =间隔-> get_end();如果(b1 == b2&& e1 == e2)返回true;返回false;}虚拟布尔is_within(Interval * interval){无符号长b1 = this-> get_begin();无符号长e1 = this-> get_end();无符号长b2 = interval-> get_begin();无符号长e2 =间隔-> get_end();如果(b1> = b2&& e1< = e2)返回true;返回false;}virtual bool contains(Interval * interval){//检查此间隔是否包含另一个间隔无符号长b1 = this-> get_begin();无符号长e1 = this-> get_end();无符号长b2 = interval-> get_begin();无符号长e2 =间隔-> get_end();如果(b1< = b2&& e1> = e2)返回true;返回false;}虚拟布尔is_empty(){返回(get_end()< get_begin())?true:false;}虚拟布尔is_intersect(Interval * interval){无符号长b1 = this-> get_begin();无符号长e1 = this-> get_end();无符号长b2 = interval-> get_begin();无符号长e2 =间隔-> get_end();如果(b1> e2)返回false;如果(b2> e1)返回false;返回true;}虚拟虚空print(){cout<<'('<< get_begin()<<','<< get_end()<<<)\ n";}};class IntInterval:公共时间间隔{私人的:无符号的长期开始;无符号的长端;IntInterval();上市:虚拟间隔* interval_copy(){返回新的IntInterval(begin,end);}IntInterval(unsigned long a,unsigned long b):开始(a),结束(b){}void set_value(unsigned long a,unsigned long b){开始= a;结束= b;}void set_begin(unsigned long a){开始= a;}无效set_end(unsigned long b){结束= b;}虚拟间隔* copy(){间隔* new_interval = new IntInterval(begin,end);返回new_interval;}虚拟无符号长get_begin(){返回开始}虚拟无符号长get_length(){返回end-begin + 1;}虚拟无符号长get_end(){返回端}虚拟间隔* get_intersect(间隔*间隔);//获取两个间隔的相交部分虚拟〜IntInterval(){};};间隔* IntInterval :: get_intersect(间隔*间隔){unsigned long begin2 = interval-> get_begin();unsigned long end2 =间隔-> get_end();if(end< begin2 || begin> end2){返回新的IntInterval(1,0);}返回新的IntInterval((begin> begin2)?begin:begin2,(end< end2)?end:end2);}IntInterval * parse_ip(const char * _str){无符号long _begin = 0;无符号长_end = 0;字符串输入(_str);如果(input.find('-')!= string :: npos){字符串开始= input.substr(0,input.find('-'));字符串结尾= input.substr(input.find('-')+ 1);无符号整数ip1 = 0,ip2 = 0;无符号整数ip3 = 0,ip4 = 0;sscanf(begin.c_str(),%u.%u.%u.%u",& ip1,& ip2,& ip3,& ip4);_begin =(ip1 << 24)+(ip2 << 16)+(ip3 << 8)+ ip4;ip1 = 0;ip2 = 0;ip3 = 0;ip4 = 0;sscanf(end.c_str(),%u.%u.%u.%u",& ip1,& ip2,& ip3,& ip4);_end =(ip1 << 24)+(ip2 << 16)+(ip3 << 8)+ ip4;如果((_begin> _end)||(_end> MAX_IP_RANGE)){cout<<错误:IP间隔错误.范围是"<<开始<<--< end<< endl;退出(0);}}返回新的IntInterval(_begin,_end);}bool compFunc(Interval * i,Interval * j){return(i-> get_begin()< j-> get_begin());}int main(){向量<向量<对<字符串,字符串>>>网;向量< pair<字符串,字符串>>X;向量< pair<字符串,字符串>>y;x.push_back(make_pair("1.1.1.1","3.0.0.0"));x.push_back(make_pair("10.2.5.3","30.2.5.0"));x.push_back(make_pair("100.2.25.2","130.2.25.2"));y.push_back(make_pair("41.0.2.2","43.2.2.5"));y.push_back(make_pair("131.2.2.2","135.5.5.2"));nets.push_back(x);nets.push_back(y);向量< IntInterval *>_nets;对于(int i = 0; i<(int)nets.size(); i ++)for(int j = 0; j<(int)nets [i] .size(); j ++){字符串s = nets [i] [j] .first +'-'+ nets [i] [j] .second;_nets.push_back(parse_ip(s.c_str()));}sort(_nets.begin(),_ nets.end(),compFunc);如果(_nets.size()> 1)对于(vector< IntInterval *> :: iterator,它= _nets.begin()+ 1;它< _nets.end();){如果((* it)-> get_begin()-1 ==(*(it-1))-> get_end()){(*(it-1))-> set_end((* it)-> get_end());_nets.erase(it);}否则if((* it)-> get_begin()-1<(*(it-1))-> get_end()){它++;cout<<错误:网络地址重叠!"<< endl;}别的它++;}对于(int i = 0; i<(int)_nets.size(); i ++)cout<<_nets [i]-> get_begin()<<"<<_nets [i]-> get_end()<<恩德尔返回0;} 

解决方案

您从未提供 virtual〜Interval(); 和其他几个函数的实现.您必须为声明的所有非纯虚函数提供一个实现.特别是,G ++会在类中发出vtable以及第一个声明的非内联函数的实现.省略其实现意味着您将没有vtable,因此将无法构造类(因此会出现这些错误).

简而言之,定义您声明的每个函数,纯虚函数除外.在某些情况下,有理由离开排除了已声明函数的定义,但是它们很少见.

There is some linking error here. I looked for that online, yet still I couldn't find the problem. How can I fix it?

g++ test.cpp -o test
/tmp/ccDfCj4N.o: In function `Interval::Interval()':
test.cpp:(.text._ZN8IntervalC2Ev[Interval::Interval()]+0x9): undefined reference to     `vtable for Interval'
/tmp/ccDfCj4N.o: In function `IntInterval::~IntInterval()':
test.cpp:(.text._ZN11IntIntervalD0Ev[IntInterval::~IntInterval()]+0x1d): undefined     reference to `Interval::~Interval()'
/tmp/ccDfCj4N.o: In function `IntInterval::~IntInterval()':
test.cpp:(.text._ZN11IntIntervalD1Ev[IntInterval::~IntInterval()]+0x1d): undefined     reference to `Interval::~Interval()'
/tmp/ccDfCj4N.o:(.rodata._ZTI11IntInterval[typeinfo for IntInterval]+0x10): undefined     reference to `typeinfo for Interval'
collect2: ld returned 1 exit status

Here is the code! All classes are in a same file for a pilot program.

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

#define MAX_IP_RANGE    4294967295

class Interval {

    public:
        virtual Interval * interval_copy() = 0;
        virtual unsigned long get_begin() = 0;
        virtual unsigned long get_end() = 0;
        virtual unsigned long get_length() = 0;
        virtual Interval*     get_intersect(Interval *interval) = 0;   // Examine whether two intervals have intersection
        virtual Interval*     copy() = 0;
        virtual ~Interval();
        virtual bool is_equal(Interval *interval) {
            unsigned long b1 = this->get_begin();
            unsigned long e1 = this->get_end();
            unsigned long b2 = interval->get_begin();
            unsigned long e2 = interval->get_end();
            if (b1 == b2 && e1 == e2)
                    return true;
            return false;
        }

        virtual bool is_within(Interval *interval) {
            unsigned long b1 = this->get_begin();
            unsigned long e1 = this->get_end();
            unsigned long b2 = interval->get_begin();
            unsigned long e2 = interval->get_end();
            if (b1 >= b2 && e1 <= e2)
                return true;
            return false;
        }

        virtual bool contains(Interval *interval) {    // Examine whether this interval contains another interval
            unsigned long b1 = this->get_begin();
            unsigned long e1 = this->get_end();
            unsigned long b2 = interval->get_begin();
            unsigned long e2 = interval->get_end();
            if (b1 <= b2 && e1 >= e2)
                return true;
            return false;
        }

        virtual bool is_empty() {
            return (get_end()<get_begin())?true:false;
        }

        virtual bool is_intersect(Interval *interval) {
            unsigned long b1 = this->get_begin();
            unsigned long e1 = this->get_end();
            unsigned long b2 = interval->get_begin();
            unsigned long e2 = interval->get_end();

            if (b1>e2)
                return false;
            if (b2>e1)
                return false;
            return true;
        }

        virtual void print()
        {
            cout << '('<<get_begin() << ',' << get_end() << ")\n";
        }
};


class IntInterval : public Interval {
    private:
        unsigned long begin;
        unsigned long end;
        IntInterval();

    public:
        virtual Interval * interval_copy() {
            return new IntInterval(begin, end);
        }

        IntInterval(unsigned long a, unsigned long b): begin (a), end  (b)
        {}

        void set_value(unsigned long a, unsigned long b) {
            begin = a;
            end = b;
        }

        void set_begin(unsigned long a) {
            begin = a;
        }

        void set_end(unsigned long b) {
            end = b;
        }

        virtual Interval* copy()
        {
            Interval *new_interval = new IntInterval(begin, end);
            return new_interval;
        }

        virtual unsigned long get_begin() {
            return begin;
        }

        virtual unsigned long get_length() {
            return end-begin+1;
        }

        virtual unsigned long  get_end() {
            return end;
        }

        virtual Interval* get_intersect(Interval *interval);   // Get the intersect part of two intervals
        virtual ~IntInterval() {};
    };

    Interval* IntInterval::get_intersect(Interval *interval) {
        unsigned long begin2 = interval->get_begin();
        unsigned long end2 = interval->get_end();
        if (end < begin2 || begin > end2) {
            return new IntInterval(1, 0);
        }
        return new IntInterval((begin>begin2)?begin:begin2, (end<end2)?end:end2);
    }


    IntInterval * parse_ip(const char * _str) {
        unsigned long  _begin=0;
        unsigned long  _end=0;
        string input(_str);
        if (input.find('-') != string::npos){
            string begin = input.substr(0, input.find('-'));
            string end = input.substr(input.find('-')+1);

            unsigned  int ip1 = 0, ip2 = 0;
            unsigned  int ip3 = 0, ip4 = 0;
            sscanf(begin.c_str(), "%u.%u.%u.%u", &ip1, &ip2, &ip3, &ip4);
            _begin = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;

            ip1 = 0; ip2 = 0; ip3 = 0; ip4 = 0;
            sscanf(end.c_str(), "%u.%u.%u.%u", &ip1, &ip2, &ip3, &ip4);
            _end = (ip1 << 24) + (ip2 << 16) + (ip3 << 8) + ip4;

            if ((_begin > _end) ||  (_end > MAX_IP_RANGE)){
                cout<<"ERROR: The IP INTERVAL IS WRONG The range is "<<begin<<"-"<<end<<endl;
                exit(0);
            }
        }
        return new IntInterval(_begin, _end);
    }

    bool compFunc (Interval * i, Interval * j) {
        return (i->get_begin() < j->get_begin());
    }

    int main () {

        vector <vector<pair<string, string> > > nets;

        vector<pair<string, string> > x;
        vector<pair<string, string> > y;

        x.push_back(make_pair("1.1.1.1", "3.0.0.0"));
        x.push_back(make_pair("10.2.5.3", "30.2.5.0"));
        x.push_back(make_pair("100.2.25.2", "130.2.25.2"));

        y.push_back(make_pair("41.0.2.2", "43.2.2.5"));
        y.push_back(make_pair("131.2.2.2", "135.5.5.2"));

        nets.push_back(x);
        nets.push_back(y);

        vector <IntInterval *> _nets;
        for (int i=0; i<(int)nets.size(); i++)
            for(int j=0; j<(int)nets[i].size(); j++) {
                string s = nets[i][j].first + '-' + nets[i][j].second;
                _nets.push_back(parse_ip(s.c_str()));
            }
        sort(_nets.begin(), _nets.end(), compFunc);

        if (_nets.size()>1)
            for (vector<IntInterval *>::iterator it = _nets.begin()+1; it < _nets.end(); ) {
                if ((*it)->get_begin()-1 == (*(it-1))->get_end()) {
                    (*(it-1))->set_end((*it)->get_end());
                    _nets.erase(it);
                }
                else if ((*it)->get_begin()-1 <  (*(it-1))->get_end()) {
                    it++;
                    cout<<"ERROR: Network address overlapping!"<<endl;
                }
                else
                    it++;
            }

        for (int i=0; i<(int)_nets.size(); i++)
            cout << _nets[i]->get_begin() << "  " << _nets[i]->get_end() << endl;

      return 0;
    }

解决方案

You never provided an implementation for virtual ~Interval(); and several other functions. You must provide an implementation for all non-pure virtual functions you declare. In particular, G++ emits the vtable along with the implementation of the first-declared non-inline function in a class. Omitting its implementation means you won't have a vtable, and thus won't be able to construct the class (hence these errors).

In short, define every function you declare, except for pure virtuals. There are some cases where it's justified to leave out the definition for a declared function, but they are very rare.

这篇关于链接错误:未定义对"vtable for XXX"的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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