please help, | SoloLearn: Learn to code for FREE!

0

please help,

can anyone solve this code #include<iostream> using namespace std; template<class T> class RGB{ public: T x,y; RGB(T tx,T ty){ x=tx; y=ty; } RGB operator-=(RGB t); RGB operator/=(RGB t); friend std::ostream & operator<< <T>(std::ostream & out, RGB<T> & ); }; template <class T> RGB <T> RGB<T>::operator-=(RGB t){ x-=t.x; y-=t.y; return *this; } template<class T> /* TODO (#1#): > */ RGB<T> RGB <T>::operator/=(RGB t){ x/=t.x; y/=t.x; return *this; } template<class T> ostream&operator<<(ostream &out,RGB<T>t){ out<<t.x<<' '<<t.y<<endl; return out; }void main(){ RGB<int>t(1,2),t1(2,3),t2(3,4); cout<<"t="<<t; cout<<"t1="<<t1; cout<<"t2="<<t2; t-=t1; cout<<"t-"<<"t1"<<t; t1/=t2; cout<<"t/"<<"t1"<<t1<<endl; RGB<float>p(1.1,2.2),p1(2.3,3.4),p2(3.5,4.6); cout<<"p="<<p; cout<<"p1="<<p1; cout<<"p2="<<p2; p1-=p; cout<<"p1-"<<"p="<<p; p2/=p1; cout<<"p1/"<<"p2"<<p1; cin.get(); }

11/4/2019 2:16:49 AM

jak Leito

1 Answer

New Answer

+4

jak Leito Change declaration of friend std::ostream & operator<<<T> (std::ostream & out, RGB<T> & ); }; This is not a member function hence it cannot be specialized for T, you either specialize it for RGB<int> and RGB<float> or make it a template function like template<class U> friend std::ostream & operator<< (std::ostream & out, RGB<U> & ); }; Note the parameter name should be different from parameter T of the class else you will get warning. Also change void main to int main and remove cin.get() if you want to run it at Sololearn.