查看单个帖子
  #1 (permalink)  
旧 2007-12-31
yxlssxy yxlssxy 当前离线
初级会员
 
注册日期: 2007-12-31
住址: 北京
帖子: 1
yxlssxy 正向着好的方向发展
发送 MSN 消息给 yxlssxy
默认 问一个菜鸟问题:范型编程的编译错误

我用VC编程,可是用VC编译稍微复杂点的模板程序,就会出错。请问我需要特殊的设置还是该选用其它的编译器?

------------------------编译错误信息-------------------------------
1>e:\代码\testtmpl\testtmpl.cpp(1 : error C2975: 'y' : invalid template argument for 'Add', expected compile-time constant expression
1> e:\代码\testtmpl\alg.hpp(4) : see declaration of 'y'
------------------------以下是代码---------------------------------
template<int x, int y>
struct Add{

static int value;
};

template<int x, int y>
struct Multiply{

static int value;

};

template<int x, int y>
int Add<x,y>::value = x + y;

template<int x, int y>
int Multiply<x,y>::value = x * y;

void main()
{
std::cout<<Add<1, int(Multiply<2, 3>::value) >::value<<"\n";
}
回复时引用此帖