problem with function overloading
Apr 11, 2013 at 3:31pm UTC
I don't know why the compiler always says the 8 `double poly<double>::x' is private
and
36 for `T poly<T>::power(int, T)' does not match any in class `poly<T>'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
T x is unaccesible.
using namespace std;
template <typename T>
class poly
{private :
T x;
int n;
int a;
public :
poly(const T x1,const int a1,const int n1)
{ n=n1;
x=x1;
a=a1;
}
T power(const int &n,const T &x);
poly&operator +(poly&p1);
void display();
~poly();
};
template <typename T>
void poly<T>::display()
{
cout<<x;
};
template <typename T>
T poly<T>::power(int n, T x)
{ int i=0;
L: if (i==n)
goto M;
x=x*x;
i++;
M:return x;
};
template <typename T>
poly<T>&poly<T> ::operator +( poly<T>&p1 )
{
x=p1.x+x;
return this ;
}
int main()
{
poly<double > P(2.5,1,2);
P.x=P.power(2,2.5);
P.display();
system("pause" );
return 0;
}
Last edited on Apr 11, 2013 at 3:37pm UTC
Apr 11, 2013 at 3:39pm UTC
I don't know why the compiler always says the 8 `double poly<double>::x' is private
1 2 3 4
{private :
T x;
int n;
int a;
`T poly<T>::power(int, T)' does not match any in class `poly<T>'
T power(const int & n,const T & x);
Last edited on Apr 11, 2013 at 3:43pm UTC
Apr 11, 2013 at 6:29pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <iostream>
using namespace std;
template <typename T>
class poly
{private :
T x;
int n;
int a;
public :
poly()
{
this ->x=0;
this ->a=0;
this ->n=0;}
poly(T x1,int a1, int n1)
{ n=n1;
x=x1;
a=a1;
}
void power();
poly&operator +(poly&);
void display();
~poly()
{};
};
template <typename T>
void poly<T>::display()
{
cout<<x;
};
template <typename T>
void poly<T>::power()
{ int i=0;
L: if (i==n)
goto M;
x=x*x;
i++;
M: x=a*x;
};
template <typename T>
poly<T>&poly<T> ::operator +( poly<T>&p1 )
{
x=p1.x+x;
return this ;
}
int main()
{
poly<double > P(2.5,1,2);
P.power();
P.display();
system("pause" );
return 0;
}
Topic archived. No new replies allowed.