下面程序通过编译了,但是无法运行
#pragma warning (disable: 4786)
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef struct tagStudentInfo
{
int nid;
string strname ;
bool operator < (tagStudentInfo const& _A) const;
}StudentInfo;
int main()
{
map<StudentInfo,int>mapStudent;
map<StudentInfo,int>::iterator iter ;
StudentInfo studentInfo;
studentInfo.nid=1;
studentInfo.strname="student_one";
mapStudent.insert(pair<StudentInfo,int>(studentInfo,90));
studentInfo.nid=2;
studentInfo.strname="student_two";
mapStudent.insert(pair<StudentInfo,int>(studentInfo,80));
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<< iter->first.nid << endl << iter->first.strname << endl<< iter->second <<endl;
}
return 0 ;
}
错误提示:map.obj : error LNK2001: unresolved external symbol "public: bool __thiscall tagStudentInfo::operator<(struct tagStudentInfo const &)const " (??MtagStudentInfo@@QBE_NABU0@@Z)
Debug/map.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
map.exe - 1 error(s), 0 warning(s)
有没有大侠知道是什么情况,还有该怎么解决
[解决办法]
bool operator < (tagStudentInfo const& _A) const;函数没有定义,给个具体定义就好了
[解决办法]
bool StudentInfo::operator<(tagStudentInfo const& _A) const
{
return nid < _A.nid;
}