返回   cpper编程论坛 > 技术杂烩
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

技术杂烩 找不到地方的技术问题?这里!

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-12-31
初级会员
 
注册日期: 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";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2008-01-01
summersnowe2006 的头像
普通会员
 
注册日期: 2006-02-14
帖子: 74
summersnowe2006 正向着好的方向发展
默认 回复: 问一个菜鸟问题:范型编程的编译错误

Multiply 里的value是static,不是编译期能够决定的。而Add template的x和y都要求编译期就要能够确定的数值。所以 std::cout<<Add<1, int(Multiply<2, 3>::value) >::value<<"\n"; 通不过编译呢。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2008-01-02
liuxinyu 的头像
高级会员
 
注册日期: 2006-02-09
帖子: 303
文章: 48
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";
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 06:51 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0