2016-10-02 42 views
0

嘿家伙我试图在C++中实现反向传播算法,我试过Xcode和C++ Eclipse,但是我得到了同样的错误,我不知道如何解决它,我试着在这里搜索,但没有建议的解决方案工作,这里是以下错误消息和我的代码。关于神经网络实现的C++错误:体系结构x86_64的重复符号

错误消息:

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 

} 
+0

有趣的是,如果我在主文件中声明Net类并删除include,代码就可以工作。但是当我尝试在类中分离文件时,出现此错误,但我不知道为什么重复正在发生 –

回答

0

您的IDE试图将.cpp文件编译为单个编译单元,因为通常这些文件包含各个编译单元。

然而要包括在NeuralNet.cppNet.cpp,所以在它的代码(即Net::Net实现)中的Net.cpp单元以及的NeuralNet.cpp单位被编译。

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

你永远不应该#include a .cpp文件。将#include "Net.cpp"替换为#include "Net.h"。您始终在课程中包含标题(.h),而不是来源(.cpp)。

+0

好吧,但是如果我删除主文件中的#include,我无法引用Net的类型,我该怎么做呢? –

+0

谢谢兄弟!现在它工作正常!我一整天都很沮丧!你摇滚! –

相关问题