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