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{co ut << inv << endl;}
private:
T inv;
};
template<class T>
class Exp
{
public:
T operator()(cons t T& base, T exp)throw(invAr g<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<lo ng 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{co ut << inv << endl;}
private:
T inv;
};
template<class T>
class Exp
{
public:
T operator()(cons t T& base, T exp)throw(invAr g<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<lo ng double>& inv)
{
inv.Write();
}
system("PAUSE") ;
return 0;
}
}
I think the type checking is too strong; any suggestions? Thanks!!!
Comment