undefined reference to vtable for inheriting classes - c++

I'm trying to develop a neural network in c++.
I refactored my code utilizing class-inheritance, to allow me to have different kinds of layers, whose neurons could be iterated over in sequence.
My problem is, that my linker (ld invoked by g++) complaines about undefined references to vtables and typeinfo-objects.
As far as I know, the error "undefined reference to vtable for class" comes from not implemented pure virtual methods in base classes, but unfortunately I not was able locate the error more percisely.
make all:
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o Test2dSparselyNeuralNet.o Test2dSparselyNeuralNet.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o NeuralNet.o NeuralNet.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o Sparsely2dNeuralNetwork.o Sparsely2dNeuralNetwork.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o Connection.o Connection.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o Layer2d.o Layer2d.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o Neuron2dIterator.o Neuron2dIterator.cpp
g++ -Wall -fmessage-length=0 --std=c++11 -w -c -o input/2dinput/cross/cross.o input/2dinput/cross/cross.cpp
g++ -o bin/neural_net_2d Test2dSparselyNeuralNet.o NeuralNet.o Sparsely2dNeuralNetwork.o Connection.o Layer2d.o Neuron2dIterator.o input/2dinput/cross/cross.o
Layer2d.o: In function `Layer::Layer()':
Layer2d.cpp:(.text._ZN5LayerC2Ev[_ZN5LayerC5Ev]+0x13): undefined reference to `vtable for Layer'
Layer2d.o: In function `Iterator::Iterator()':
Layer2d.cpp:(.text._ZN8IteratorC2Ev[_ZN8IteratorC5Ev]+0xf): undefined reference to `vtable for Iterator'
Layer2d.o: In function `Layer::~Layer()':
Layer2d.cpp:(.text._ZN5LayerD2Ev[_ZN5LayerD5Ev]+0x13): undefined reference to `vtable for Layer'
Layer2d.o:(.rodata._ZTI7Layer2d[_ZTI7Layer2d]+0x10): undefined reference to `typeinfo for Layer'
Neuron2dIterator.o:(.rodata._ZTI16Neuron2dIterator[_ZTI16Neuron2dIterator]+0x10): undefined reference to `typeinfo for Iterator'
collect2: error: ld returned 1 exit status
make: *** [neural_net_2d] Error 1
makefile:
CXXFLAGS = -Wall -fmessage-length=0 --std=c++11 -w
CXXFLAGS_DEBUG = -g -Wall -fmessage-length=0 --std=c++11 -w
SAMPLES = input/2dinput/cross/cross.o
OBJS = Test2dSparselyNeuralNet.o NeuralNet.o Sparsely2dNeuralNetwork.o Connection.o Layer2d.o Neuron2dIterator.o ${SAMPLES}
LIBS =
TARGET = neural_net_2d
$(TARGET): $(OBJS)
$(CXX) -o bin/$(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
debug: $(OBJS)
$(CXX) -o bin/$(TARGET) $(OBJS) $(LIBS) $(CXXFLAGS_DEBUG)
clean:
rm -f $(OBJS) $(TARGET)
Layer.h
#ifndef LAYER_H
#define LAYER_H
#include "Neuron.h"
#include "Iterator.h"
class Layer
{
protected:
// biasNeuron
Neuron biasNeuron = 1.0;
public:
inline Layer() : biasNeuron(1.0) {}
virtual Iterator& start();
virtual Neuron& front(void);
virtual Neuron& back(void);
virtual int size();
protected:
private:
};
#endif // LAYER_H
Layer2d.h
#ifndef LAYER2D_H
#define LAYER2D_H
#include "Layer.h"
class Layer2d : public Layer
{
public: std::vector<std::vector<Neuron> > _neurons;
public:
Layer2d();
virtual ~Layer2d();
Iterator& start();
Neuron& front(void);
Neuron& back(void);
int size();
protected:
private:
};
#endif // 2DLAYER_H
Layer2d.cpp
#include "Layer2d.h"
#include "Neuron2dIterator.h"
class Neuron2dIterator;
Iterator& Layer2d::start(void)
{
Neuron2dIterator& it = (*new Neuron2dIterator(*this));
return it;
}
Neuron& Layer2d::front(void)
{
Neuron& frontNeuron = this->_neurons.front().front();
return frontNeuron;
}
Neuron& Layer2d::back(void)
{
Neuron& backNeuron = this->_neurons.back().back();
return backNeuron;
}
int Layer2d::size(void)
{
int size = this->_neurons.back().size() * this->_neurons.size();
return size;
}
Layer2d::Layer2d()
{
//ctor
}
Layer2d::~Layer2d()
{
//dtor
}
LineLayer.h
#ifndef LINELAYER_H
#define LINELAYER_H
#include "Layer.h"
#include "Neuron.h"
#include <vector>
using namespace std;
class LineIterator;
class LineLayer : public Layer
{
public: std::vector<Neuron> _neurons;
public:
LineLayer();
protected:
private:
};
#endif // LINELAYER_H
LineLayer.cpp
#include "LineLayer.h"
#include "LineIterator.h"
Iterator& LineLayer::start()
{
LineIterator& it = (*new LineIterator());
return it;
}
Neuron& LineLayer::front()
{
Neuron& frontNeuron = this->_neurons.front();
return frontNeuron;
}
Neuron& LineLayer::back()
{
Neuron& backNeuron = this->_neurons.back();
return backNeuron;
}
int LineLayer::size()
{
return this->_neurons.size();
}
LineLayer::LineLayer()
{
//ctor
}
LineLayer::~LineLayer()
{
//dtor
}
Thnx for help!

As explained at https://gcc.gnu.org/wiki/VerboseDiagnostics#missing_vtable the vtable will be in the same object file as the first non-inline virtual function, which is Layer::start. You haven't defined that function, so the compiler never generated the vtable.
To fix the linker error be sure you have provided a definition for the first non-inline virtual function declared in the class.

Related

Unrelated method of an object is called when creating a pointer to the object

I have a class called BinaryNode and I want to create a pointer to a specific instance of it. However, in this line of code when I set the pointer the debugger shows that it calls a totally unrelated method called getRight for some reason.
template <typename ItemType, typename Keytype>
void BST<ItemType, Keytype>::recAdd(ItemType entry, BinaryNode<ItemType>* subtree)
{
if (subtree->getEntry() == entry)
{
throw(runtime_error("Error: recAdd attempted with a duplicate value.\n"));
}
else if (subtree->getEntry() < entry)
{
if (subtree->getRight() == nullptr)
{
BinaryNode<ItemType>* temp = nullptr; // The pointer in question
temp = new BinaryNode<ItemType>(entry);
subtree->setRight(temp);
}
else
{
recAdd(entry, subtree->getRight());
}
}
else
{
if (subtree->getLeft() == nullptr)
{
BinaryNode<ItemType>* temp = nullptr;
temp = new BinaryNode<ItemType>(entry);
subtree->setLeft(temp);
}
else
{
recAdd(entry, subtree->getLeft());
}
}
}
Here is getRight in the BinaryNode implementation file:
template<typename T>
BinaryNode<T>* BinaryNode<T>::getRight() const
{
return(m_right);
}
Here is a picture of the debugger
Any help would be greatly appreciated.
Makefile:
pokedex: main.o Pokemon.o Executive.o Tester.o
g++ -O0 -g -std=c++11 -Wall main.o Pokemon.o Executive.o Tester.o -o pokedex
main.o: main.cpp
g++ -O0 -g -std=c++11 -Wall -c main.cpp
Pokemon.o: Pokemon.h Pokemon.cpp
g++ -O0 -g -std=c++11 -Wall -c Pokemon.cpp
Tester.o: Tester.h Tester.cpp
g++ -O0 -g -std=c++11 -Wall -c Tester.cpp
Executive.o: Executive.h Executive.cpp BST.h BST.cpp BinarySearchTreeInterface.h BinaryNode.h BinaryNode.cpp
g++ -O0 -g -std=c++11 -Wall -c Executive.cpp
clean:
rm *.o pokedex

Makefile issue with Class template: clang warning linker input unused

I have 2 classes: a class template list.t with definition and implementation, and ticker.h and ticker.cpp, along with a driver program, main.cpp. I want to compile main.cpp to get to main.x which uses both the ticker and list class. This is my makefile so far.
# makefile for fx project
CC = g++
CFLAGS = -g -Wall -Wextra
default: main.x
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -o $# main.o list.o ticker.o
list.o: list.t
$(CC) -c list.t
ticker.o: ticker.cpp
$(CC) -c ticker.cpp list.t
main.o: list.t ticker.cpp main.cpp
$(CC) -c main.cpp ticker.cpp list.t
But on executing the command make I am getting the following error:
make
g++ -c main.cpp ticker.cpp list.t
clang: warning: list.t: 'linker' input unused
g++ -c list.t
clang: warning: list.t: 'linker' input unused
g++ -g -Wall -Wextra -o main.x main.o list.o ticker.o
clang: error: no such file or directory: 'list.o'
make: *** [main.x] Error 1
List.t - (without implementations)
#ifndef LIST_T
#define LIST_T
#include <iostream>
template <typename T>
class List
{
public:
// constructors
List();
List(T);
List(const List&);
~List();
// member functions
List& operator = (const List&);
void PushFront (const T&);
void PushBack (const T&);
T PopFront();
T PopBack();
T& Front();
T& Back();
const T& Front() const;
const T& Back() const;
size_t Size() const;
bool Empty() const;
void Clear();
void Display (std::ostream&, char = '\0') const;
//private vars
private:
class Link
{
Link (const T& t) : element_(t), nextLink_(0), previousLink_(0) {};
T element_;
Link* nextLink_;
Link* previousLink_;
friend class List<T>;
};
Link* firstLink_;
Link* lastLink_;
};
I am sure this is a simple error, and I have scoured google for this error message, but I either am not fully understanding their solutions, or they are not working for me. Either way, let me know if you have a solution to this problem, or any other comments on the quality and structure of this makefile. Also any knowledge on why my flags are apparently being unused would be greatly appreciated!
Thanks!
You need -c in the command line only when you compile source files to create object files.
Change
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -c main.o list.o ticker.o
to
main.x: main.o ticker.o list.o
$(CC) $(CFLAGS) -o $# main.o list.o ticker.o
^^^^^
$# is the file name of the target of the rule.
See GNU make: Automatic Variables for more such variables.
The makefile should be the following:
CC = g++
CPPFLAGS = -g -Wall -Wextra
default: main.x
main.x: main.o ticker.o
$(CC) -o $# main.o ticker.o
ticker.o: ticker.cpp
$(CC) -c ticker.cpp $(CPPFLAGS)
main.o: list.t main.cpp
$(CC) -c main.cpp $(CPPFLAGS)
You don't compile a template file. The implementation should be inside the same file as the class declaration (putting the .hpp extension would be better also). Then the template must be included in the other files. For example, in the main, put a #include "list.t" directive.

Failed to create first library. `undefined reference to `main`

I am attempting to create my first linux based static library. I am following the directions from this site here.
This is the code
#ifndef _ADDNUMBERS_H
#define _ADDNUMBERS_H
class AddNumbers
{
private:
int _a;
int _b;
public:
AddNumbers ();
~AddNumbers ();
void setA (int a);
void setB (int b);
int getA () const;
int getB () const;
int getSum () const;
}; // AddNumbers
#endif // _ADDNUMBERS_H
~/workspace/C++/AddNumbers/src/AddNumbers.cpp
#include "AddNumbers.h"
AddNumbers::AddNumbers ()
: _a(0), _b(0)
{
}
AddNumbers::~AddNumbers ()
{
}
void AddNumbers::setA (int a)
{
_a = a;
}
void AddNumbers::setB (int b)
{
_b = b;
}
int AddNumbers::getA () const
{
return _a;
}
int AddNumbers::getB () const
{
return _b;
}
int AddNumbers::getSum () const
{
return _a + _b;
}
This is the makefile I am using . The header and cpp files are next to each other
CC=g++
ifeq ($(DEBUG),yes)
CXXFLAGS=-Wall -g
LDFLAGS=-Wall -g
else
CXXFLAGS=-Wall
LDFLAGS=-Wall
endif
AR=ar
ARFLAGS=rcs
INC=AddNumbers.h
SRC=AddNumbers.cpp
OBJ=AddNumbers.o
OUT=libAddNumbers.a
INCLUDES= -I #./$(INCPATH)
default: $(OUT)
$(OUT): $(OBJ)
$(AR) $(ARFLAGS) $# $(OBJ) # The # means use the target name
%.o: %.cpp $(INC)
$(CC) $(CXXFLAGS) $(INCLUDES) -c $< -o $#
.PHONY: clean cleanall
clean:
rm -f *.o
cleanall: clean
rm -f $(OUT)
This is my output
admin#localhost lib$ make
g++ -Wall -I -c AddNumbers.cpp -o AddNumbers.o
/usr/lib/gcc/x86_64-redhat-linux/4.8.3/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [AddNumbers.o] Error 1
admin#localhost lib$
How can I fix this issue ?
I believe your argument-less -I is swallowing your -c argument so gcc doesn't see it and use compile-only mode.
Either give your -I argument a value or get rid of it and see if that works.

Makefile C++ inheritance

The following are the files involved and a short description:
arrayListType.h, arrayListTypeImp.cpp: declare and implement arrayListType class and its functions.
unorderedarrayListType.h unorderedArrayListTypeImp.cpp: inherit arrayListType class and declare unorderedarrayListType class and implement virtual functions of arrayListType class.
Ch13_Ex6.cpp: Instantiates an object of class unorderedArrayListType and runs some tests.
I am having a compilation error, which I think is due to the Makefile. The following is the error:
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope
Line 4 has a using namespace std; command. The line before that is #include "arrayListType.h". I have tried the following variations in the Makefile but neither worked:
Version 1
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall arrayListTypeImp.o unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6
Version 2:
all: Ch13_Ex6
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
Ch13_Ex6: arrayListTypeImp.o unorderedArrayListTypeImp.o Ch13_Ex6.o
g++ -Wall Ch13_Ex6.o arrayListTypeImp.o unorderedArrayListTypeImp.o -o Ch13_Ex6
Both versions compile arrayListTypeImp.o and give the error shown above when compiling unorderedArrayListTypeImp.o. The following is the complete compile output:
make
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
arrayListTypeImp.o
unorderedArrayListType.h:16: error: expected unqualified-id at end of input
unorderedArrayListTypeImp.cpp:4: error: expected unqualified-id before 'using'
unorderedArrayListTypeImp.cpp: In member function 'virtual void unorderedArrayListType::insertAt(int, int)':
unorderedArrayListTypeImp.cpp:11: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:11: error: 'endl' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'cout' was not declared in this scope
unorderedArrayListTypeImp.cpp:13: error: 'endl' was not declared in this scope
Code for arrayListType.h:
#ifndef H_arrayListType
#define H_arrayListType
class arrayListType
{
public:
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, int item) const;
virtual void insertAt(int location, int insertItem) = 0;
virtual void insertEnd(int insertItem) = 0;
void removeAt(int location);
int retrieveAt(int location) const;
virtual void replaceAt(int location, int repItem) = 0;
void clearList();
virtual int seqSearch(int searchItem) const = 0;
virtual void remove(int removeItem) = 0;
arrayListType(int size = 100);
arrayListType(const arrayListType& otherList);
virtual ~arrayListType();
protected:
int *list;
int length;
int maxSize;
};
#endif
Code for unorderArrayListTypeImp.cpp:
#include <iostream>
#include "unorderedArrayListType.h"
using namespace std;
void unorderedArrayListType::insertAt(int location,
int insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
void unorderedArrayListType::insertEnd(int insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
// More virtual functions implemented and finally a constructor
unorderedArrayListType::unorderedArrayListType(int size)
: arrayListType(size)
{
} //end constructor
You did not #include <iostream> in arrayListType.h, but did it in arrayListType.cpp, before you #include "arrayListType.h" there. You need to place #include <iostream> into arrayListType.h before you use std::cout or std::endl.
To avoid such mistakes it is good to place the interface header as the first #include statement int the implementation file.
I am guessing that the error is in unorderedArrayListType.h, you likely have a missing semicolon or something. Looking in the Makefile will do nothing to solve that error.
EDIT: Woah, there actually is something wrong with your Makefile! Heh, I just looked at it and you have the following:
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
Don't pass .h files to g++!, only the .cpp files. So write it like this:
g++ -c -Wall arrayListTypeImp.cpp
Likewise:
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
should be:
g++ -c -Wall unorderedArrayListTypeImp.cpp
Evan Teran is probably right. expected unqualified-id before '...' usually means you missed a semicolon before that line. If it turns out it really is your makefile, here's some general makefile advice:
Use variables. Instead of
g++ -c -Wall ...
you can do
CXX_OPTS= -Wall -O3
...
g++ -c $(CXX_OPTS) ...
Use pattern rules. Rather than
arrayListTypeImp.o: arrayListType.h arrayListTypeImp.cpp
g++ -c -Wall arrayListType.h arrayListTypeImp.cpp
unorderedArrayListTypeImp.o: unorderedArrayListType.h unorderedArrayListTypeImp.cpp
g++ -c -Wall unorderedArrayListType.h unorderedArrayListTypeImp.cpp
Ch13_Ex6.o: Ch13_Ex6.cpp
g++ -c -Wall Ch13_Ex6.cpp
you can use
%.o:%.cpp
g++ $(CXX_OPTS) -c $< -o $#
This should shorten your makefile and make it easier to debug and find your problem. If you want more information, check this out.

ffs does not name a type in C++

I get compilation error when i try to compile the following..... Please comment.
Here is my code:
ffs.h
#ifndef FFS_H
#define FFS_H
#include <iostream>
#include <string>
#include "commands.h"
class ffs{
private:
Filesystem *filesys;
Superblock block;
void processCommands(InputParser command);
void performQuit();
void performInit(InputParser command);
public:
void acceptCommands();
ffs(){};
};
#endif
ffs.cpp
#include "ffs.h"
void ffs::acceptCommands(){
std::string input;
while(true){
std::cout<< "Enter command : ";
getline(std::cin,input);
InputParser parser(input);
processCommands(parser);
}
}
void ffs::performInit(InputParser command){
command.getCommand().pop_front();
int n = atoi(command.getCommand().front().c_str());
std::cout<< n << " : number of blocks "<<std::endl;
command.getCommand().pop_front();
int m = atoi(command.getCommand().front().c_str());
std::cout<<m << " : number of inode blocks" << std::endl;
command.getCommand().pop_front();
block.ninode=m;
Filesystem fs(n);
filesys = &fs;
}
void ffs::performQuit(){
///filesys->clean();
exit(0);
}
void ffs::processCommands(InputParser command){
std::string cmd=command.getCommandName();
if(cmd.compare(commands::Q())==0) performQuit();
else if (cmd.compare(commands::INIT())==0) performInit(command);
}
tester.h
#ifndef TESTER_H
#define TESTER_H
#include "ffs.h"
class ffs;
class tester{
private:
ffs ffsobj;
public:
void run(){ffsobj.acceptCommands()};
};
#endif
tester.cpp
#include "tester.h"
int main()
{
tester runner;
runner.run();
return 0;
}
ERROR:
g++ -c -Wall tester.cpp
tester.h:7: error: ffs does not name a type
tester.h: In member function void tester::run():
tester.h:9: error: ffsobj was not declared in this scope
tester.h:9: error: expected `;' before ˜} token
make: *** [tester.o] Error 1
Makefile:
CFLAGS=-c -Wall
CC=g++
all: flags.o InputParser.o commands.o Filesystem.o ffs.o tester.o
$(CC) flags.o InputParser.o commands.o Filesystem.o ffs.o tester.o -o runner.o
flags.o : flags.cpp flags.h
$(CC) $(CFLAGS) flags.cpp
InputParser.o : InputParser.cpp InputParser.h
$(CC) $(CFLAGS) InputParser.cpp
ffs.o: ffs.cpp ffs.h
$(CC) $(CFLAGS) ffs.cpp
commands.o: commands.cpp commands.h
$(CC) $(CFLAGS) commands.cpp
Filesystem.o: Filesystem.cpp Filesystem.h Superblock.h
$(CC) $(CFLAGS) Filesystem.cpp
tester.o: tester.cpp tester.h ffs.o
$(CC) $(CFLAGS) tester.cpp
#fileUtility.o : IFileUtility.h
# gcc -c IFileUtility.h
# Inode.h commands.h Filesystem.h
clean :
rm *.o
You can't use ffs as your class name, it already has a meaning in c++ (albiet an obscure one). Just pick a different name.
You got a #endif in your ffs.h file, without the befinning #if.
If you include ffs.h in your tester.h file, why do you declare class ffs; ?
Error is in following line:
class ffs;
class tester{
private:
ffs ffsobj; // <--- can't declare object for ffs; can be pointer/reference only
To declare an object of ffs type, class tester should be able to see its full definition. Otherwise you can just declare a pointer or reference of ffs type, if you have forward declared like that (ffs* or ffs&).
Remove the forward class declaration class ffs; from tester.h. It is unnecessary since you include ffs.h and then confuses the compiler when it tries to define a class member which it thinks it has not seen the definition for.
do not solve the "does not name a type" problem, but look at this line:
void run(){ffsobj.acceptCommands()};
the correct is:
void run(){ffsobj.acceptCommands();}