+ 5
How to define a template specialization for a friend function or operator?
I wish to overload a friend function or a friend operator in a different way for a data type. How can I achieve the same ?
33 Respuestas
+ 9
need to be able to bookmark posts on the phone. i know i will need to do this one day
+ 9
Nice Idea. And whilst I am here..
Kinshuk: Well done! I had faith in you the whole time!
+ 8
No Eye Deer.. But does this help?
http://stackoverflow.com/questions/4421706/operator-overloading
+ 8
p.s when you find out? can you share a code example with comments? cause you know, you are awesome!
+ 7
@ɐısıօլɐ
It is finally done!
I finally completed it!
This is what I used:
// Forward declare the class
template <class M>
class Matrix;
// Forward declare the template operator
template <typename M>
ostream& operator<<(ostream& out, const Matrix<M>& matrix);
// Forward declare the function
ostream& operator<<(ostream& out, const Matrix<Complex>& mc);
//Now the Matrix Class :
template<class M>
class Matrix
{
//Many useful Functions...
friend std::ostream& operator<<(std::ostream& out, const Matrix<Complex>& matrix ) //Function Specialization
{
for(int i=0;i<mc.r;i++)
for(int j=0;j<mc.c;j++)
{
cout<<"Element at "<<i<<","<<j<<" - "<<mc.mat[i][j]<<endl;
}
}
friend std::ostream& operator<< <>(std::ostream& out, const Matrix<M>& matrix ) //Original Template Overload...
{
for(int i=0;i<matrix.r;i++)
for(int j=0;j<matrix.c;j++)
cout<<matrix.mat[i][j];
}
}
Thanks a Ton for your help!
+ 6
Yes, as anyone would prefer << over a print function in C++ atleast...
+ 6
I really forgot the & in the specialization 😅.
But on putting it, i am left with just the second error I mentioned before...
+ 6
So one must force declare the class and its function for specializations...
Yes, it must be made as an instance...
+ 6
:) aloisia is smart too! but you never gave up either
+ 5
Yes, making a print function and overloading works, but only if its not a friend...
I guess Ill have to adjust with the print function for this task...
+ 5
well the first error says you should've written template<class T> before the operator<< declaration, I don't know if that'll work though if it is inside the Matrix class
+ 5
Why is that required though when there is template<class M> before the class?
+ 5
or I just saw a solution on stack overflow that looks like this, maybe it'll work for you?
friend ostream& operator<< <T>(ostream& o, const Matrix<T>& mat)
+ 5
Ill try it then...
+ 5
that's required because it is not a method of the class Matrix but just a friend function and you can't specialize the function over the class Matrix, because it is not an instance of it
+ 5
It didn't work...
Perhaps I am not able to implement it as said...
+ 5
Amazing, I'm glad I could help you :D Cheers! 🍻
+ 5
@jay maybe share the link to the discussion to yourself and copy it into a memo :D
+ 4
@jay
Sorry, but that didn't help as its just a detailed explanation for operator overloading...
I want to know how I can overload the friend operators or functions of a template class for a data type like double or char...
+ 4
@jay
Sorry, but that didn't help...
I wish to know a way so I can overload a friend function of a template class for a particular data type like double or char.