头文件的疑问error LNK1120: 2 unresolved externals

striveforit 2009-08-12 05:54:13
里面的加载的头文件是照着教材上写的,我自己加了#include<string>和using namespace std;部分
compile没问题,就是build时出现
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall File<struct IndexItem,struct ElemType>::Append(char *,char *,struct ElemType)" (?Append@?$File@UIndexItem@@UElemType@@@@QAEXPAD0UElemType@@@Z)
main.obj : error LNK2001: unresolved external symbol "public: struct ElemType __thiscall File<struct IndexItem,struct ElemType>::inputElem(struct ElemType &)" (?inputElem@?$File@UIndexItem@@UElemType@@@@QAE?AUElemType@@AAU2@@Z)
Debug/mywork.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
小的实在菜,这是怎么一回事呢?希望众高手支招

//main.cpp
#include<string>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include"file.h"
void main()
{ char *p1=".\\File.dat",*p2=".\\File.idx";
File<IndexItem,ElemType>myfile;
ElemType e; //记录元素

while(1){
cout<<"欢迎\n";
cout<<"1-增加记录\t 2-删除记录\t 3-查找记录\n 4-浏览记录\n ";
char choice;
cin>>choice;
switch(choice)
{case '1':
myfile.inputElem(e);
cout<<e.key<<endl<<e.length ;
myfile.Append(p1,p2,e);break;
}
}//end while
}


//file.h
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
#include<iostream.h>
#include<string>
using namespace std;

typedef char KeyType;

//主文件的记录类型
struct ElemType{
KeyType key[10]; //关键字域
string content; //内容域
int length; //内容长度
};

//索引文件的记录类型
struct IndexItem{
KeyType key; //关键字域
int next; //对应记录位置
};

template<class T,class T1> //T==IndexItem;T1==ElemType
class File
{public:

ElemType inputElem(ElemType & e);

void Append(char*fn1,char*fn2,T1 );

};

//Elem.cpp
#include"file.h"
template<class T,class T1> //T==IndexItem;T1==ElemType
ElemType inputElem(T1 & e)
{
......
}

...全文
379 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
striveforit 2009-08-12
  • 打赏
  • 举报
回复
问题解决啦,网上搜了下,
也许是头文件.h与没有.h并有using namespace std的会出现cout的二义性问题
我把#include<iomanip.h>改为#include<iomanip>后问题解决了
不过不清楚为什么会这样就是。。
striveforit 2009-08-12
  • 打赏
  • 举报
回复
谢谢大家的回答~
#include"file.h"
using namespace std; 调换了,可还是一样。。
还是main中出现大量'cout' : ambiguous symbol和'cin' : ambiguous symbol
怎么办??
mstlq 2009-08-12
  • 打赏
  • 举报
回复
main.cpp头文件是
#include <stdlib.h> 
#include <iomanip.h>
#include <string>
using namespace std;
#include"file.h"


————>

#include <stdlib.h> 
#include <iomanip.h>
#include <string>
#include"file.h"


using namespace std;
striveforit 2009-08-12
  • 打赏
  • 举报
回复
我照楼上方法试了,main中出现大量'cout' : ambiguous symbol和'cin' : ambiguous symbol
main.cpp头文件是
#include<stdlib.h>
#include<iomanip.h>
#include<string>
using namespace std;
#include"file.h"

file.h头文件是
#include<stdlib.h>
#include<stdio.h>
#include<fstream> //.h
#include<iostream> //.h
#include<stdlib.h>
#include<iomanip.h>
#include<string>
using namespace std;
是不是这里面哪些用多了有冲突了,怎么解决好呢?
mstlq 2009-08-12
  • 打赏
  • 举报
回复
可以啊,注意控制字符长度不要超过9就好了^_^

我自己倾向于写成下面的样子,效果是一样的^_^

#define keylenth 10
typedef char KeyType[keylenth];

//主文件的记录类型
struct ElemType{
KeyType key; //关键字域
string content; //内容域
int length; //内容长度
};
striveforit 2009-08-12
  • 打赏
  • 举报
回复
谢谢mstlq~~
顺便问一下,以下定义的key定义得对吗?我原意是key是定长的字符串

//记录关键字类型
typedef char KeyType;

//主文件的记录类型
struct ElemType{
KeyType key[10]; //关键字域
string content; //内容域
int length; //内容长度
};
mstlq 2009-08-12
  • 打赏
  • 举报
回复
将Elem.cpp里面的内容全部搬到file1.h……
然后在硬盘上删除Elem.cpp,编译……

或者在file.h里面最后一行写#include"Elem.cpp"
然后在工程里面移除Elem.cpp,编译……
striveforit 2009-08-12
  • 打赏
  • 举报
回复
详细得说明一下行吗?
对应模板实在不清楚怎么使用
striveforit 2009-08-12
  • 打赏
  • 举报
回复
小的实在愚钝,那把File<IndexItem,ElemType>myfile;加到file.h里面吗?还要做什么修改呢?
xingzhe2001 2009-08-12
  • 打赏
  • 举报
回复
File类只有定义没有实现阿?
striveforit 2009-08-12
  • 打赏
  • 举报
回复
不好意思,忘了贴上void Append(char*fn1,char*fn2,T1 ); 的函数了,它的状况跟
//Elem.cpp
#include"file.h"
template<class T,class T1> //T==IndexItem;T1==ElemType
ElemType inputElem(T1 & e)
{
......
}
一样,所以就没再贴了
mstlq 2009-08-12
  • 打赏
  • 举报
回复
模板的实现请放入头文件^_^

如果放在cpp里——也行,不过请注意,这时候不能在别的cpp里面用哦……
coverallwangp 2009-08-12
  • 打赏
  • 举报
回复
将file.h和Elem.cpp放到一个文件中
老的编译器不支持模板的声明和定义放到两个文件中
fishion 2009-08-12
  • 打赏
  • 举报
回复
//main.cpp
#include<string>
#include<stdlib.h>
#include<iostream.h>
#include<fstream.h>

#include<iomanip.h>
#include"file.h"
void main()
重复的去掉
yshuise 2009-08-12
  • 打赏
  • 举报
回复
void Append(char*fn1,char*fn2,T1 );
这个没有实现,要实现也得放在.h,而不是.cpp里面。因为是模板。

65,189

社区成员

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

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