I don't understand why the compiler keeps saying there's an error when I tried to access the "put" function from my overloaded operator "<<". Could someone tell me why I can't access them, and my compiler is Microsoft Visual C++ 2010. Thanks in advance
It works for me (with gcc/linux).
Try putting the declaration of put above that of operator<<.
You could still make it private (although I don't see why it should be).
Thank you, tqb. I've tried your solution but it still gives me the error that the function put is inaccessible. I guess that's just the problem of the compiler.
Yes, it works when I make it public but I believe that a friend function can access the class private member variables and private member functions right?
And the error is function "SortedList<T,length>::put" (declared at line 40) is inaccessible
By the way, this is unrelated to your problem, but note that your put() implementation writes its output to std::cout, rather than to its 'out' parameter.
Yes, it works when I make it public but I believe that a friend function can access the class private member variables and private member functions right?
Yes, of course. I was just trying to get more information. I give up.
(Although I doubt the error said "in accessible" since it should be one word.)
Yeah, sorry about that. Since I could not copy the error message directly so I have to rewrite it. Anyway, I think it just the matter of compiler because I tried it on pythontutor and it works perfectly fine. Thanks everyone
Did you try what helios said above my last post?
Although you may have to do it more like:
1 2 3 4 5 6 7 8 9
// inside the class
template <typename T2, int length2>
friend ostream& operator<<(ostream& out, const SortedList<T2,length2>& list);
// after the class
template <typename T, int length>
ostream& operator<<(ostream& out, const SortedList<T,length>& list) {
return list.put(out);
}
Yeah, I believe is the compiler. I have to use it because I'm a university student and my school tests are based on this compiler. Thank you for trying to help.
Finally the explanation! I was thinking that "inaccessible" wasn't a normal-sounding error. A real error would have mentioned something about it being private.