C ++将错误与多个.cpp文件链接起来

时间:2013-12-01 09:11:09

标签: c++ header visual-studio-2013

这是我的头文件。如果我只将它包含在一个.cpp文件中,它就可以正常使用,但只要我尝试将它包含在两个或更多.cpp文件中,只要地图包含在标题中就会产生错误。我不做任何事情,只能在.cpps中调用该文件。

#ifndef _TOOLS_H_
#define _TOOLS_H_
#include <iostream>
#include <map>
#include <sstream>
#include <string>

using namespace std;

map<char, string> d_one;
map<char, string> d_ten;

void smain();
string converter(int i);

string one(string in);
string two(string in);
string three(string in);
string four(string in);
string five(string in);
string six(string in);
string seven(string in);
string eight(string in);
string nine(string in);
string ten(string in);
#endif

这是Visual Studio 2013给我的错误:

1>  Dictionary.cpp
1>  Funcion.cpp
1>  Main.cpp
1>Funcion.obj : error LNK2005: "class std::map<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<char>,class std::allocator<struct std::pair<char const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > d_ten" (?d_ten@@3V?$map@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@D@2@V?$allocator@U?$pair@$$CBDV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@2@@std@@A) already defined in Dictionary.obj
1>Funcion.obj : error LNK2005: "class std::map<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<char>,class std::allocator<struct std::pair<char const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > d_one" (?d_one@@3V?$map@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@D@2@V?$allocator@U?$pair@$$CBDV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@2@@std@@A) already defined in Dictionary.obj
1>Main.obj : error LNK2005: "class std::map<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<char>,class std::allocator<struct std::pair<char const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > d_ten" (?d_ten@@3V?$map@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@D@2@V?$allocator@U?$pair@$$CBDV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@2@@std@@A) already defined in Dictionary.obj
1>Main.obj : error LNK2005: "class std::map<char,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<char>,class std::allocator<struct std::pair<char const ,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > d_one" (?d_one@@3V?$map@DV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$less@D@2@V?$allocator@U?$pair@$$CBDV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@std@@@2@@std@@A) already defined in Dictionary.obj
1>MSVCRT.lib(crtexe.obj) : error LNK2001: unresolved external symbol _main
1>C:\Users\Vincent\Google Drive\Programming\C++\Challange\Release\Challange.exe : fatal error LNK1120: 1 unresolved externals

1 个答案:

答案 0 :(得分:5)

这是extern的用途。

在您的标头文件中:

extern map<char, string> d_one;
extern map<char, string> d_ten;

这说“这些地图存在于其他地方”(而不是“创建这些地图”)。

在一个(恰好一个)源文件中:

map<char, string> d_one;
map<char, string> d_ten;