查看单个帖子
  #3 (permalink)  
旧 2008-01-02
liuxinyu 的头像
liuxinyu liuxinyu 当前离线
高级会员
 
注册日期: 2006-02-09
帖子: 311
文章: 49
liuxinyu 正向着好的方向发展
默认 回复: 问一个菜鸟问题:范型编程的编译错误

应该是
const static int
请参阅我去年的文章:
http://www.cpper.com/site/comments/scheme_cpp_template/
如果用vc的话,vc6以后的都可以跑我的例子。
引用:
作者: 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";
}
回复时引用此帖