Exponent程序出现问题

Exponent程序出现问题

本文介绍了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;

}

}


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

解决方案




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


抛出invArg< T> (基础);


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


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


Martin。






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

你是温和的娱乐。


祝你好运,


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!!!

解决方案



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.





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

Best regards,

Tom


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

08-18 22:04