读书人

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

发布时间: 2012-12-30 10:43:15 作者: 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;
}

------解决方案--------------------


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.h文件中就可以了。
[解决办法]
把Compare.cpp的代码全放到Compare.h里去
[解决办法]
#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中去掉#include "Compare.h"
[解决办法]
引用:
将Compare.cpp的代码全放到Compare.h里去是可以运行,可是老师规定类的声明和定义必须分开,他说这么写才规范,怎么办啊???????


在c++中定义的模板类,及其实现要放在一起。不光是模板类是这样,模板函数也是如此。 现在想想可能是模板函数或模板类本身不能直接生成代码,而必须和特定的数据类型结合后才能生成代码的缘故吧。而CPP文件一般是要生成OBJ文件的。应用了模板的函数即使看起来似乎已经实现了,但实际上塔仍然属于一种声明。如下例:
template <class T>
int compare(T t1,T t2){
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
个人觉得这也只是一个声明,在编译器进行编译的时候并不能立刻生成obj
又如:
在头文件 A.h
template <class T>
class A(){
int compare(T t1,T t2);
}
在cpp A.cpp中
template <class T>

int A:: compare(T t1,T t2)
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
编译器一般是通不过的。在编译的时候,cpp文件需要生成.o文件,而模板类在其实现时是未有办法生成.o文件的。一个方法就是把模板类的实现直接放在.h文件中。并且在某些编译器中还必需在声明时就实现才能通过 如下所示:
//A.hpp
template <class T>
class A{
int compare(T t1,T t2)
{
if(t1>t2) return 1;
if(t1==t2) return 0;
if(t1<t2)return -1;
}
}
}

读书人网 >C++

热点推荐