读书人

error LNK2019: 无法解析的外部符号

发布时间: 2012-02-11 09:51:35 作者: rapoo

error LNK2019: 无法解析的外部符号 ),该符号在函数 _main 中被引用
问题: error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<char>::Compare<char>(char,char)" (??0?$Compare@D@@QAE@DD@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: float __thiscall Compare<float>::min(void)" (?min@?$Compare@M@@QAEMXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: float __thiscall Compare<float>::max(void)" (?max@?$Compare@M@@QAEMXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<float>::Compare<float>(float,float)" (??0?$Compare@M@@QAE@MM@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall Compare<int>::min(void)" (?min@?$Compare@H@@QAEHXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall Compare<int>::max(void)" (?max@?$Compare@H@@QAEHXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Compare<int>::Compare<int>(int,int)" (??0?$Compare@H@@QAE@HH@Z),该符号在函数 _main 中被引用
1>D:\vc++2005\程序\9-14\Debug\9-14.exe : fatal error LNK1120: 9 个无法解析的外部命令

主函数文件:
#include <iostream>

#include "Compare.h"
using namespace std;

int main()
{
Compare<int> cmp1(3,7);
cout << cmp1.max() << " is the Maximum of two inteder numbers." << endl;
cout<< cmp1.min() << " is the Minimum of two inteder numbers." << endl << endl;
Compare<float> cmp2(45.78,93.6);
cout << cmp2.max() << " is the Maximum of two float numbers." << endl;
cout << cmp2.min() << " is the Minimum of two float numbers." << endl << endl;
Compare<char> cmp3('a','A');
cout << cmp3.max() << " is the Maximum of two characters." << endl;
cout << cmp3.min() << " is the Minimum of two characters." << endl;
return 0;
}
头文件声明的类:
#pragma once
#include <iostream>
using namespace std;

template <class T>
class Compare
{
public:
Compare(T a, T b);
public:
T max(void);
T min(void);
private:
T x,y;
};
类的定义:
#include <iostream>
using namespace std;
#include "Compare.h"

template <class T>
Compare<T>::Compare(T a, T b)
{
x = a;
y = b;
}

template <class T>
T Compare<T>::max(void)
{
return (x > y) ? x:y;
}

template <class T>
T Compare<T>::min(void)
{
return (x < y) ? x:y;
}


[解决办法]

C/C++ code
template <class T>Compare<T>::Compare(T a, T b){x = a;y = b;}    template <class T>T Compare<T>::max(void){return (x > y) ? x:y;}template <class T>T Compare<T>::min(void){return (x < y) ? x:y;}
[解决办法]
把Compare.cpp的代码全放到Compare.h里去
[解决办法]
C/C++ code
#pragma once#include <iostream>using namespace std;template <class T>class Compare{public:Compare(T a, T b);public:T max(void);T min(void);private:T x,y;};#include "Compare.cpp"  //这里引用。。。
[解决办法]
探讨
将Compare.cpp的代码全放到Compare.h里去是可以运行,可是老师规定类的声明和定义必须分开,他说这么写才规范,怎么办啊???????


读书人网 >C++

热点推荐