I'm new in using templates and also overloaded operators for them. This is my simple code. I tried to write an operator<< for the type T, but faced some odd error!
I didn't understand what you said. I'm not a native speaker of English.
But about tags, there are some problems when creating a new topic such as: tags don't work, preview feature doesn't work and so on. These problems occur often. At the beginning these occurred for me but when editing I could use them properly. so I made the question clear for better reading.
How to solve the problem without using friend? I'm not used to it.
Given that there is no way for any function outside of your class to access its private members, you must supply that access, either through a member function or the use of friend.
Why not: ...(ostream&os, const T& to) { ... please? Both T and S are user-defined types and new.
There may be some confusion here. Within the class T refers to the type S is instantiated with. In your parameterized functions outside the class, T refers to the type the functions are called with. They are not the same.
So, if you were to use T& inside the class definition and you instantiated the class with type int (as your example does in main) then you would be defining an overload on type int& for operator<< when a perfectly adequate overload already exists, since int is a built-in type.
Given that there is no way for any function outside of your class to access its private members, you must supply that access, either through a member function or the use of friend.
But that operator is inside the class not outside of it. What if we make valpublic?
Within the class T refers to the type S is instantiated with.
I read it many times but couldn't understand!
The matter is somewhat complex. I'm reading the book of Mr. Stroustrup which seems to be hard for beginners.
But that operator is inside the class not outside of it. What if we make val public?
That operator is not a member of the class, so it doesn't have access to variables and methods that do not have a public access level. I only defined it inside the class, because it is easier to supply the definition there than outside the class.
How do you define that operator outside the class for that code please?
if you were to use T& inside the class definition and you instantiated the class with type int (as your example does in main) then you would be defining an overload on type int& for operator<< when a perfectly adequate overload already exists, since int is a built-in type.
Would you write an example for that use of int&s?
Most of the types I use are built-in types, such as: int, double, char...