神经网络实现中的c++错误:体系结构x86_64的重复符号



嘿,伙计们,我试图在c++中实现反向传播算法,我尝试过Xcode和c++ Eclipse,但我得到了同样的错误,我不知道如何修复它,我尝试在这里搜索,但没有提出的解决方案工作,这里是以下错误信息和我的代码。

错误信息:

make all 
Building file: ../src/NeuralNet.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/NeuralNet.d" -MT"src/NeuralNet.d" -o"src/NeuralNet.o" "../src/NeuralNet.cpp"
Finished building: ../src/NeuralNet.cpp
Building target: NeuralNet
Invoking: MacOS X C++ Linker
g++  -o "NeuralNet"  ./src/Net.o ./src/NeuralNet.o   
duplicate symbol __ZN3NetC2ERKNSt3__16vectorIjNS0_9allocatorIjEEEE in:
    ./src/Net.o
    ./src/NeuralNet.o
duplicate symbol __ZN3NetC1ERKNSt3__16vectorIjNS0_9allocatorIjEEEE in:
    ./src/Net.o
    ./src/NeuralNet.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [NeuralNet] Error 1

下面是我的代码:

NeuralNet.cpp

#include <iostream>
#include <vector>
#include "Net.cpp"
using namespace std;
int main() {
    vector<unsigned> topology;
    topology.push_back(10);
    Net net(topology);

    return 0;
}

Net.h

#ifndef NET_H_
#define NET_H_
#include <vector>
using namespace std;
class Net {
public:
    Net(const vector<unsigned>& topology);
};
#endif

Net.cpp

#include "Net.h"
Net::Net(const vector<unsigned>& topology) {
    // TODO Auto-generated constructor stub
}

您的IDE正在尝试将两个.cpp文件编译为单独的编译单元,因为通常它们包含单独的编译单元。

但是您将Net.cpp包含在NeuralNet.cpp中,因此其中的代码(即Net::Net的实现)在Net.cpp单元和NeuralNet.cpp单元中编译。

编译完两个编译单元后,调用链接器将它们链接在一起。它注意到Net::Net出现了两次,不知道该选择哪个,因此出现了错误。

你不应该#include一个.cpp文件。将#include "Net.cpp"替换为#include "Net.h"。总是包含类的头文件(.h),而不是源文件(.cpp)。

最新更新