有两个文件代码,一个aa.h,一个bb.h,如下:
//aa.h
#ifndef _AA_H_
#define _AA_H_
//#include"bb.h" //这行代码出问题
template<class T1,class T2>
T1 cvtType(T2 x)
{
}
#endif
//bb.h
#ifndef _BB_H_
#define _BB_H_
#include"aa.h"
template<class T>
void func(T x)
{
cvtType(x);
}
#endif
如果加上include"bb.h"就会报错如下:
如果不加上就能编译成功。 虽然说删掉include"bb.h"就能解决问题,但是我还是想了解一下背后的原因。 已经加上了ifndef,为什么还是报错。 求解答。
因为BB里边调用了 cvtType(x);这个函数
你那样引用的话函数cvtType(x)声明与定义的顺序产生了问题
与include"bb.h"没关系,你的模板函数定义的有问题,把模板函数改成普通函数再试试