对于继承类的vtable的未定义引用

时间:2014-08-01 09:01:43

标签: c++ linker g++ neural-network ld

我正在尝试用c ++开发神经网络。

我使用类继承重构了我的代码,允许我拥有不同类型的层,其神经元可以按顺序迭代。

我的问题是,我的链接器(由g ++调用的ld)抱怨对vtable和typeinfo-objects的未定义引用。

据我所知,错误“对vtable for class的未定义引用”来自基类中未实现的纯虚方法,但不幸的是我无法更加精确地找到错误。

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

生成文件:

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寻求帮助!

1 个答案:

答案 0 :(得分:4)

https://gcc.gnu.org/wiki/VerboseDiagnostics#missing_vtable所述,vtable与第一个非内联虚函数位于同一目标文件中,即Layer::start。您尚未定义该函数,因此编译器从未生成vtable。

  

要修复链接器错误,请确保已为类中声明的第一个非内联虚函数提供了定义。