整数打印机无法编译 [英] Integer printer won't compile

查看:66
本文介绍了整数打印机无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我弄清楚为什么这个整数打印程序不会被b
编译?我在诊断方面看得很远,找出原因,

但我迷路了。这是代码:


#include< iostream>

#include< cstdlib>

using namespace std ;


模板< int I>

class _name

{

public:

static void f()

{

static int i = 0;

cout<< i<< endl;

i ++;

_name< go?(I-1):0> :: f();

}

私人:

enum {go =(I-1)!= 0};

};


//专业化为

//递归提供基本案例

模板<>

class _name< 0>

{

public:

static void f(int i){return;}

};


int main()

{

//等效循环代码

_name< 5> :: f();

系统(PAUSE);

返回0;

}


任何帮助,请?谢谢!!!!!

Can you help me figure out why this integer printing program won''t
compile? I''ve looked far and wide in my diagnostics to figure out why,
but I''m lost. Here''s the code:

#include <iostream>
#include <cstdlib>
using namespace std;

template<int I>
class _name
{
public:
static void f()
{
static int i=0;
cout << i << endl;
i++;
_name<go?(I-1):0>::f();
}
private:
enum {go=(I-1)!=0};
};

// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(int i){return;}
};

int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE");
return 0;
}

Any help, please? Thanks!!!!!

推荐答案

好的,我修复了编译概率,但现在,而不是打印0-5,它

打印全0。而且它是静止的!!!!!

OK, I fixed the compiling prob, but now, instead of printing 0-5, it
prints all 0s. And it''s static !!!!!


" Protoman" <镨********** @ gmail.com>在新闻中写道:1140576150.527670.67460

@ f14g2000cwb.googlegroups.com:
"Protoman" <Pr**********@gmail.com> wrote in news:1140576150.527670.67460
@f14g2000cwb.googlegroups.com:
好的,我修复了编译概率,但现在,而不是打印0- 5,它打印所有0。它是静态的!!!!!


a)包括你所指的文字...很难引用

正确。

#include< iostream>
#include< cstdlib>
使用命名空间std;

模板< int I>
类_name
{
public:
static void f()
{
static int i = 0;
cout<< i<< endl;
i ++;
_name< go?(I-1):0> :: f();
}
私人:
enum {go =( I-1)!= 0};
};

//专业化提供
//递归的基本案例
模板<>
类_name< 0>
{
公开:
static void f(int i){return;}
};

int main()
{
//等效循环代码
_name< 5> :: f();
系统(" PAUSE");
返回0;
}
OK, I fixed the compiling prob, but now, instead of printing 0-5, it
prints all 0s. And it''s static !!!!!

a) Include the text that you are referring to... makes it hard to quote
properly.
#include <iostream>
#include <cstdlib>
using namespace std;

template<int I>
class _name
{
public:
static void f()
{
static int i=0;
cout << i << endl;
i++;
_name<go?(I-1):0>::f();
}
private:
enum {go=(I-1)!=0};
};

// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(int i){return;}
};

int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE");
return 0;
}




b)因为它是静态的,你有5个本地变量实例i,

让我们把它们命名为如下:


_name< 5> :: f :: i

_name< 4> :: f :: i

_name< ; 3> :: f :: i

_name< 2> :: f :: i

_name< 1> :: f :: i


每一个都被初始化为零,并且增加到1,但你永远不会看到它。


c)为什么不是'你只是使用模板功能而不是整个

类吗?


d)发布可编译的运行代码。你的不是。在_name< 0>中没有名为f的函数
实例。


e)什么是去导致这个问题?它似乎没有任何有用的东西。


f)如果你修复所有这些,你将得到0-4。



b) Because it''s static, you have 5 instances of the local variable i,
let''s name them as follows:

_name<5>::f::i
_name<4>::f::i
_name<3>::f::i
_name<2>::f::i
_name<1>::f::i

Each one is initialized to zero, and is incremented to 1, but you never
see it.

c) Why aren''t you just using a template function instead of an entire
class?

d) Post compilable, running code. Yours doesn''t. There is no function
named f in the _name<0> instance.

e) What is "go" contributing to this problem? It does not appear to do
anything useful.

f) And if you fix all of these, you''ll get 0-4.


Andre Kostur< nn ****** @ kostur.net>写在

新闻:Xn ****************************** @ 207.35.177.1 34:< br>
Andre Kostur <nn******@kostur.net> wrote in
news:Xn******************************@207.35.177.1 34:
" Protoman" <镨********** @ gmail.com>写在
新闻:1140576150.527670.67460 @ f14g2000cwb.googlegroups.com:
"Protoman" <Pr**********@gmail.com> wrote in news:1140576150.527670.67460 @f14g2000cwb.googlegroups.com:
//专业化提供
//递归的基本案例
模板<>
class _name< 0>
{
public:
static void f(int i){return;}
};

int main( )
{
//等效循环代码
_name< 5> :: f();
系统(暂停);
返回0;
}
// Specialization provides base case for
// recursion
template<>
class _name<0>
{
public:
static void f(int i){return;}
};

int main()
{
// Equivalent loop code
_name<5>::f();
system("PAUSE");
return 0;
}


d)发布可编译的运行代码。你的不是。在_name< 0>中没有名为f的函数。实例。


d) Post compilable, running code. Yours doesn''t. There is no function
named f in the _name<0> instance.




错误...我的意思是没有名为f *的函数在_name中没有参数*

< 0>实例。



Err... I meant "no function named f *taking no parameters* in the _name
<0> instance".


这篇关于整数打印机无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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