【发布时间】:2020-07-05 06:57:31
【问题描述】:
我是个新手,在我的 c++ 代码中遇到了一些非常奇怪的错误。据我所知,它们是由于多个包含错误造成的。
我有以下文件
CardBase.h
#include <string>
#include <vector>
#include <map>
class Class1 {
string someString;
vector<type> someVector;
map<type,type> someMap;
type someMethod (param);
}
CardBase.cpp
#include "StringParser.cpp"
someType Class1::someMethod (param){
// Use splitAtChar()
}
StringParser.cpp
#include <string>
#include <vector>
someType splitAtChar(){
...
}
这会在 VS 代码中产生两个错误:
LNK2005 "类 std::vector
,class std::allocator >,class std::allocator ,class std::allocator > > __cdecl splitAtChar(class std::basic_string ,class std::allocator >,char)" (?splitAtChar@@YA?AV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@ V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@ V?$allocator@D@2@@2@D@Z) 已经在 CardBase.obj 中定义
和
找到一个或多个多重定义的符号
【问题讨论】:
-
为什么会这样?当编译器找到
#include "file"时,它会将包含替换为文件中任何内容的副本。 -
这能回答你的问题吗? One or more multiply defined symbols found