继承链接引发'undefined reference to...' C++



我在C++写了一个"大"索引项目,这对我来说很困难......当我尝试在 2 个分支之间创建继承时:

ZonalPermutant wich 继承自 Permutant

我收到以下错误:

错误:

g++ -Wall -std=c++0x lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o -o permZone
lib/Permutant.o: In function `Permutant::Permutant()':
Permutant.cpp:(.text+0x20): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(long)':
Permutant.cpp:(.text+0x8e): undefined reference to `vtable for Permutant'
lib/Permutant.o: In function `Permutant::Permutant(PList<long>*, long)':
Permutant.cpp:(.text+0x10c): undefined reference to `vtable for Permutant'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant()':
ZonalPermutant.cpp:(.text+0x41): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(long)':
ZonalPermutant.cpp:(.text+0xa4): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::ZonalPermutant(PList<long>*, PList<long>*, long)':
ZonalPermutant.cpp:(.text+0x13e): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o: In function `ZonalPermutant::~ZonalPermutant()':
ZonalPermutant.cpp:(.text._ZN14ZonalPermutantD2Ev[_ZN14ZonalPermutantD5Ev]+0x2f): undefined reference to `Permutant::~Permutant()'
lib/ZonalPermutant.o:(.rodata._ZTI14ZonalPermutant[_ZTI14ZonalPermutant]+0x10): undefined reference to `typeinfo for Permutant'
collect2: error: ld returned 1 exit status
Makefile:17: recipe for target 'permZone' failed
make: *** [permZone] Error 1

我知道可能是我的制作文件中的链接可能写错了。所以我会给你看我的制作文件:

制作文件:

CC = g++
STD = -std=c++0x
DIR = -I .
CFLAGS = -Wall -c $(STD) 
LFLAGS = -Wall $(STD)
BRUTEFORCE_LIB = lib/VectorSpace.o lib/Vector.o lib/BruteForce.o lib/BruteForceMain.o
PIVOT_LIB = lib/VectorSpace.o lib/Vector.o lib/Pivot.o lib/PivotMain.o
PERM_LIB = lib/Permutants.o lib/VectorSpace.o lib/Vector.o lib/PermMain.o lib/Permutant.o
BASICS_LIB = lib/MajorOrderHeap.o lib/MinorOrderHeap.o lib/PList.o lib/OList.o lib/PList.o lib/HeapElement.o lib/Random.o lib/Tokenizer.o lib/Matrix.o
PERMZONE_LIB = lib/PermZone.o lib/VectorSpace.o lib/Vector.o lib/PermZoneMain.o lib/Permutant.o lib/ZonalPermutant.o
default: permZone
#EXE's
#PermZone
permZone: $(PERMZONE_LIB)
    $(CC) $(LFLAGS) $(PERMZONE_LIB) -o permZone
lib/PermZoneMain.o: src/PermZoneMain.cpp src/Index.h src/Space.h
    $(CC) src/PermZoneMain.cpp $(CFLAGS) -o lib/PermZoneMain.o
lib/PermZone.o: src/Indexes/PermZone/PermZone.h src/Indexes/PermZone/PermZone.cpp $(BASICS_LIB)
    $(CC) src/Indexes/PermZone/PermZone.cpp $(CFLAGS) -o lib/PermZone.o
lib/ZonalPermutant.o: src/Indexes/PermZone/ZonalPermutant.cpp src/Indexes/PermZone/ZonalPermutant.h lib/Permutant.o
    $(CC) src/Indexes/PermZone/ZonalPermutant.cpp  $(CFLAGS) -o  lib/ZonalPermutant.o
lib/Permutant.o: src/Element.h src/Indexes/Permutants/Permutant.h src/Indexes/Permutants/Permutant.cpp $(BASICS_LIB)
    $(CC) src/Indexes/Permutants/Permutant.cpp $(CFLAGS) -o lib/Permutant.o

现在是 h 和 cpp 文件(我知道它有很多文本):

Permutant.h:

//
//  Created by Maximiliano Verdugo on 28/12/15.
//  Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../../Element.h"
#include "../../Basics/PList.h"
#ifndef PERMUTANT_H
#define PERMUTANT_H
class Permutant : public Element
{
protected:
    PList<long> permutation;//stores only ID's
public:
    bool isInverted;
    Permutant();
    ~Permutant();
    Permutant(long id);
    Permutant(PList<long>* permutation,long id);
    void setPermutation(PList<long>* permutation);
    PList<long> getPermutation();
    void invertPermutation();
    long distance(Permutant* other);
    string toString();
    static long spearmanFootRule(Permutant &p1, Permutant &p2);
};
#endif // PERMUTANT_H

置换.cpp:

//
//  Created by Maximiliano Verdugo on 28/12/15.
//  Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "Permutant.h"
//I dont like this trick.. but i have to use it for future distance calculations
typedef long (*P_distance)(Permutant&,Permutant&);//i hope it doesn't cause problems with inheritance :D
P_distance p_distance;
Permutant::Permutant()
{
    isInverted = false;
    p_distance = &spearmanFootRule;
}
Permutant::Permutant(long id)
{
    this->id = id;
    isInverted = false;
    p_distance = &spearmanFootRule;
}
Permutant::Permutant(PList<long>* permutation,long id)
{
    this->id = id;
    isInverted = false;
    this->permutation = *permutation;
    p_distance = &spearmanFootRule;
}
void Permutant::setPermutation(PList<long>* permutation)
{
    this->permutation = *permutation;
}
PList<long> Permutant::getPermutation()
{
    return this->permutation;
}
void Permutant::invertPermutation()
{
    PList<long> *inverted_permutation = new PList<long>(permutation.size());
    inverted_permutation->toArray();
    for (long i = 0; i < permutation.size(); ++i)
    {
        (*inverted_permutation)[permutation[i]] = i;
    }
    this->setPermutation(inverted_permutation);
    this->isInverted = !isInverted;
}
long Permutant::distance(Permutant* other)
{
    return  p_distance(*this, *other);
}
string Permutant::toString()
{
    ostringstream oss;
    oss << ((isInverted)?"i":"")<< " " << permutation.toString();
    return oss.str();
}
long Permutant::spearmanFootRule(Permutant &p1, Permutant &p2)
{
    long dist = 0;
    if(p1.isInverted == p2.isInverted)
    {
        p1.invertPermutation(); 
    }
    for (int i = 0; i < p2.getPermutation().size(); ++i)
    {
        dist+= abs(p1.getPermutation().get(p2.getPermutation().get(i)) - i);
    }
    return dist;
}

区域排列:

//
//  Created by Maximiliano Verdugo on 28/12/15.
//  Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "../Permutants/Permutant.h"
#ifndef ZONAL_PERMUTANT_H
#define ZONAL_PERMUTANT_H
class ZonalPermutant :public Permutant
{
private:
    PList<long> zones;
public:
    ZonalPermutant();
    ~ZonalPermutant(){};
    ZonalPermutant(long id);
    ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id);
    void setZones(PList<long>* zones);
    PList<long> getZones();
    long distance(Permutant* other);
    string toString();
};
#endif // ZONAL_PERMUTANT_H

ZonalPermutant.cpp:

//
//  Created by Maximiliano Verdugo on 28/12/15.
//  Copyright © 2016 Maximiliano Verdugo. All rights reserved.
//
#include "ZonalPermutant.h"
ZonalPermutant::ZonalPermutant() : Permutant()
{}
ZonalPermutant::ZonalPermutant(long id) : Permutant(id)
{}
ZonalPermutant::ZonalPermutant(PList<long>* permutation, PList<long>* zones, long id) : Permutant(permutation,id)
{
    this->zones = *zones;
}
void ZonalPermutant::setZones(PList<long>* zones)
{
    this->zones = *zones;
}
PList<long> ZonalPermutant::getZones()
{
    return this->zones;
}
long ZonalPermutant::distance(Permutant* other)
{
    return 0;
}
string ZonalPermutant::toString()
{
    return ":D";
}

我的代码中使用的所有其他类都很好地实现了,并且在编译和链接其他索引时它们不会产生任何问题......如果我的代码或编程方式有任何问题,我希望你:)对我说。谢谢你的帮助。

尝试声明析构函数为虚拟(如果没有必要,通常不会造成伤害),和/或将它们的实现添加到.cpp文件中。根据我的经验

对"vtable for Permutant"的未定义引用

暗示这个方向。

相关内容

最新更新