Exponent程序出现问题 [英] Problem with Exponent program

查看:63
本文介绍了Exponent程序出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个程序中遇到编译错误,这是关于invArg的

实例;这里是代码:


#include< iostream>

#include< cstdlib>

使用命名空间std ;


模板< class T>

class invArg

{

public:

invArg(T& arg):inv(arg){}

virtual void Write()const {cout<< inv<< endl;}

私人:

T inv;

};


模板< class T> ;

class Exp

{

public:

T operator()(const T& base,T exp )throw(invArg< T>)

{

if(exp< 0)

1 / operator()(base,exp);

else if(base == 0)

throw invArg< T>(base);

else

{

T ret = 1;

for(; exp--;)

base * = exp;

返回ret;

}

}

};


int main()

{

for(;;)

{

try

{

long double base,exp;

cout<< 输入基数和指数: << endl;

cin>> base>> exp;

cout<< base<<" ^" << exp<< " =" <<固定<< Exp< long

double>()(base,exp)<<结束;

}

catch(invArg< long double>& inv)

{

inv.Write ();

}

system(PAUSE);

返回0;

}

}


我认为类型检查过于强烈;有什么建议?谢谢!!!

解决方案

Protoman写道:

我收到编译错误了程序,关于invArg的实现;这里是代码:


" Something about ...对错误信息的描述不是很好。

模板< class T>
类invArg
{
公开:
invArg(T& arg) ):inv(arg){}
virtual void Write()const {cout<< inv<<私有:
T inv;
};




构造函数应该使用`const T&`。它现在不是,这就是为什么


抛出invArg< T> (基础);


无法编译; `base`是`const T&`。


我也会考虑让构造函数`显式`。


Martin。


这是新代码;现在我输入数字后,程序关闭。

我该怎么办?


#include< iostream>

#include< cstdlib>

使用命名空间std;


模板< class T>

class invArg

{

public:

显式invArg(const T& arg):inv(arg){}

virtual void Write() const {cout<< inv<< endl;}

私人:

T inv;

};


模板< class T> ;

class Exp

{

public:

T operator()(const T& base,T exp )throw(invArg< T>)

{

if(exp< 0)

1 / operator()(base,exp);

else if(base == 0)

throw invArg< T>(base);

else

{

T ret = 1;

for(; exp--;)

exp * = base;

返回ret;

}

}

};


int main()

{

尝试

{

long double base,exp;

cout< < 输入基数和指数: << endl;

cin>> base>> exp;

cout<< base<<" ^" << exp<< " =" <<固定<< Exp< long

double>()(base,exp)<< endl;

system(PAUSE);

返回0;

}

catch(invArg< const long double>& inv)

{

inv.Write();

}

}


有什么建议吗?谢谢!!!


Protoman写道:

这是新代码;现在我输入数字后,程序关闭。
我该怎么办?


首先,请妥善缩进你的程序。当一切都在左边缘时,你会非常难以接受你的逻辑。


其次,我们怎么知道你不知道我们这些数字是什么?

导致了这种行为?

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

模板< class T>
类invArg
公共:
显式invArg(const T& arg):inv(arg){}
virtual void Write()const {cout<< inv<<私有:
T inv;
};

模板< class T>
类Exp
{
public:
T operator()(const T& base,T exp)throw(invArg< T>)
{
if(exp< 0)
1 / operator() (碱,EXP);


上面应该怎么做?你错过了返回

的声明吗?在任何情况下,如果exp为负数,那么在你超出堆栈之前,你会得到一个无限循环

。请用上面的

解释你的意图。

else if(base == 0)
throw invArg< T>(base);
否则
{ret / = T; =
对于(; exp--;)
exp * = base;
return ret;


???您将ret初始化为1,然后您永远不会更改ret。因此,

以上将始终返回1.同时,exp变得越来越大 -

它将如何变为零?

}
}
};

int main()
{
尝试
{
long double base,exp;
cout<< 输入基数和指数: << endl;
cin>> base>> exp;
cout<< base<<" ^" << exp<< " =" <<固定<< Exp< long
double>()(base,exp)<< endl;
系统(暂停);
返回0;
}
catch(invArg< const long double>& inv)
{
inv.Write();


可能你应该在这里插入另一个系统(PAUSE),万一你的

程序抛出(我认为它确实如此)。

}
}




你知道,我认为你很可能只是一个巨魔,但我想

你是温和的娱乐。


祝你好运,


Tom


I''m getting a compilation error with this program, something about the
instation of invArg; here''s the code:

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

template<class T>
class invArg
{
public:
invArg(T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
base*=exp;
return ret;
}
}
};

int main()
{
for(;;)
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
}
catch(invArg<long double>& inv)
{
inv.Write();
}
system("PAUSE");
return 0;
}
}

I think the type checking is too strong; any suggestions? Thanks!!!

解决方案

Protoman wrote:

I''m getting a compilation error with this program, something about the
instation of invArg; here''s the code:
"Something about..." is not a very good description of an error message.
template<class T>
class invArg
{
public:
invArg(T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};



The constructor should take `const T &`. It doesn''t now and that is why

throw invArg<T> (base);

fails to compile; `base` is `const T &`.

I would also consider making the constructor `explicit`.

Martin.


Here''s the new code; now after I type the numbers, the program closes.
What do I do?

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

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
}
}

Any suggestions? Thanks!!!


Protoman wrote:

Here''s the new code; now after I type the numbers, the program closes.
What do I do?
First, please indent your program properly. You make it VERY hard to
follow your logic when everything is on the left margin.

Second, how can we know if you don''t tell us what the numbers are that
resulted in the behavior?
#include <iostream>
#include <cstdlib>
using namespace std;

template<class T>
class invArg
{
public:
explicit invArg(const T& arg):inv(arg){}
virtual void Write()const{cout << inv << endl;}
private:
T inv;
};

template<class T>
class Exp
{
public:
T operator()(const T& base, T exp)throw(invArg<T>)
{
if(exp<0)
1/operator()(base,exp);
What on earth is the above supposed to do? Are you missing a return
statement? In any case, if exp is negative, you get an infinite loop
until you overrun your stack. Please explain what you intended with
the above.
else if(base==0)
throw invArg<T>(base);
else
{
T ret=1;
for(;exp--;)
exp*=base;
return ret;
??? You initialize ret to 1, then you never change ret. Therefore, the
above will always return 1. Meanwhile, exp gets bigger and bigger -
how will it ever get to zero?
}
}
};

int main()
{
try
{
long double base,exp;
cout << "Enter a base and an exponent: " << endl;
cin >> base >> exp;
cout << base <<"^" << exp << "=" << fixed << Exp<long
double>()(base,exp) << endl;
system("PAUSE");
return 0;
}
catch(invArg<const long double>& inv)
{
inv.Write();
Probably you should insert another system("PAUSE") here, in case your
program throws (which I assume it did).
}
}



You know, I think it''s fairly likely you''re just a troll, but I suppose
you are mildly entertaining.

Best regards,

Tom


这篇关于Exponent程序出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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