关于外部变量的,急问!!!!!!!!!

rabbit729 2007-08-01 10:43:01
小弟想在程序中使用一个外部的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"选项。
...全文
157 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
gfxiang 2007-08-01
  • 打赏
  • 举报
回复
extern map<string,string> map_uuid_mxf;
只是在头文件里声明map_uuid_mxf是一个外部变量,但没有定义
所以你还得在test.cpp文件中定义这个变量
map<string,string> map_uuid_mxf;
mymtom 2007-08-01
  • 打赏
  • 举报
回复
在test.cpp加一句:
map<string,string> map_uuid_mxf;

64,652

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧