关于外部变量的,急问!!!!!!!!!
小弟想在程序中使用一个外部的map,程序共有三个文件:test.h,test.cpp,main.cpp代码如下:
/************ test.h ******************/
#ifndef TEST_H
#define TEST_H
#include <afx.h>
#include <iostream>
#include <string>
#include <utility>
#pragma warning (disable:4786)
using namespace std;
#include <map>
typedef pair<string, string> uuid_mxf;
typedef map<string, string>::value_type valType;
extern map<string,string> map_uuid_mxf; //定义一个外部map,问题所在处,
//不知如何声明外部的map
void MapMxf(map<string, string>& uuidmxf);
class Test
{
public:
Test()
{
}
Test(string str)
{
m_str = str;
}
string GetName()
{
return map_uuid_mxf[m_str];
}
private:
string m_str;
};
#endif
/************* test.cpp ******************/
#include "test.h"
void MapMxf(map<string, string>& uuidmxf)
{
CFileFind finder;
BOOL bWorking = finder.FindFile("*.cpp");
while (bWorking)
{
bWorking = finder.FindNextFile();
if (!finder.IsDots())
{
uuid_mxf temp(string(finder.GetFileName, string(finder.GetFileName()));
uuidmxf.insert(valType(temp));
cout << (LPCTSTR) finder.GetFileName() << endl;
}
}
}
/********** main.cpp ****************/
#include "test.h"
void main()
{
MapMxf(map_uuid_mxf);
Test temp("main.cpp");
cout<<temp.GetName()<<endl;
}
我这个程序就是想实现如下功能:事先通过MFC的CFileFind类建立当前目录下的.cpp的文件名和其索引(此处为试验简单,用索引对都是文件名)的一个对应关系,然后在Test类的GetName函数中可以通过索引值直接得到文件名。
现在的问题是:我把这三个文件放到一个文件中,并且将extern map<string,string> map_uuid_mxf;语句中的extern去掉,程序可以运行。如果将这三个文件分别独立的放并且加上extern就会出现如下链接错误:
Linking...
main.obj : error LNK2001: unresolved external symbol "class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,stru
ct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > map_uuid_mxf" (?map_uuid_mxf@@3V?
$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V12@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)
Debug/test8011.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
现在我觉得是这个外部的map声明存在问题,请各位高手给帮忙看一下,谢谢!
注:我得程序是在VC6.0下建立的控制台程序,为了能够使用MFC类,需要在头文件中添加#include <afx.h>并且在project->setting中设置"USE MFC"选项。