模板功能导致错误 [英] Template function resulting in error

查看:104
本文介绍了模板功能导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取错误:

Getting the error:

error C2027: use of undefined type 'T'



包括具有以下定义的模板标题:


While including a template header with the following definition:

//** Maximum value in a vector **//
template <typename T> 
T max(vector<T>& input,int* index_of_maximum = NULL)
{
	vector<T>::iterator i = input.begin();
	T maximum = *i;
	int index = 0;

	for(i++; i!=input.end(); i++){
		if(*i>maximum)
			maximum = *i;
		index++;
	}

	if(index_of_maximum)
		*index_of_maximum = index-1;

	return maximum;
}





不确定为什么编译器会调用该错误。我有一个项目工作正常,另一个项目导致该错误。必须是导致此奇怪错误的一些设置。有什么建议吗?



Not sure why the compiler is calling that an error. I have one project where that works fine and another project where it results in that error. Must be some setting that results in this odd error. Any suggestions?

推荐答案

在windef.h中有一个宏定义,其名称为max()导致了这个问题。代码很好,只需要一个不会与任何MS定义的宏冲突的不同名称。
There's a macro definition in windef.h that's name max() that resulted in this problem. The code is fine, just need a different name that won't conflict with any MS defined macros.


这篇关于模板功能导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆