I've writing a "big" Indexes project in C++, that is difficult for me meanwhile... While I was trying to create an inheritance between 2 classes:
ZonalPermutant which inherit from Permutant
I got the following error:
error:
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
I know it is probably that the linking in my makefile could be wrong written. So I will show you my 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
and now the h and cpp files (I know it's a lot of text):
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 don't 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
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";
}
Every of the other classes used in my code are good implemented and they don't create any problem at the moment of compiling and linking others Indexes... If there is any problem in my code or way to program, I would like that you say that to me. Thanks you for the help.
Try declaring your destructors virtual (usually no harm done if not necessary), and/or add their implementations to .cpp files. In my experience
Undefined reference to `vtable for Permutant'
hints to this direction.