继承链接抛出对...的未定义引用'C ++

时间:2016-01-22 22:46:45

标签: c++ inheritance makefile undefined-reference

我用C ++编写了一个“大”的索引项目,对我来说很难... 虽然我试图在2个clases之间创建一个继承:

ZonalPermutant,它继承自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

我知道可能是我的makefile中的链接写错了。所以我会告诉你我的makefile:

生成文件:

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

Permutant.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;
}

ZonalPermutant:

//
//  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";
}

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

1 个答案:

答案 0 :(得分:1)

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

  

未定义对'vtable for Permutant&#39;

的引用

暗示了这个方向。