c++ - 对继承类的虚表的 undefined reference

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

我正在尝试用 C++ 开发一个神经网络。

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

我的问题是,我的链接器(由 g++ 调用的 ld)提示对 vtable 和类型信息对象的 undefined reference 。

据我所知,错误“undefined reference to vtable for class”是由于基类中没有实现纯虚方法,但不幸的是我无法更准确地定位错误。

全部制作:

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)

层.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
}

线层.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

线层.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
}

谢谢帮助!

最佳答案

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

To fix the linker error be sure you have provided a definition for the first non-inline virtual function declared in the class.

关于c++ - 对继承类的虚表的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25076549/

相关文章:

c++ - ubuntu平台poco库链接错误

c++ - 奇怪的 mingw 链接器错误与 boost ?

c++ - 为什么在 std::vector 上删除提升迭代器

c - 使用 makefile 编译 gcc 时出现链接错误

c++ - 从串行窗口解析 json 响应

c - 如何编译需要其他dll的dll

c++ - 链接错误 VC++

c++ - 对未对齐的内存访问发出警告?

c++ - boost asio async_read() 似乎跳过了一些空值

c++ - 任意浮点值与无穷大相比如何?