error LNK2019: 无法解析的外部符号 ),该符号在函数 _main 中被引用
开开心心 2011-07-11 09:23:00 问题: 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;
}