Can't access private member functions through friend function

Hi everyone,

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

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
  #include <iostream>
using namespace std;

template <typename T, int length>
class SortedList
{
public:
	SortedList()
	{size = 0;}
	void insert(T item);
	friend ostream& operator<<(ostream& out, const SortedList& list)
	{return list.put(out);}
private:
	ostream& put(ostream& out) const;
	T list[length];
	int size;
};

template<typename T, int length>
void SortedList<T, length>::insert(T item)
{
	if (size == length){
		printf("List Full"); return; }
	int i = size - 1;
	while (i >= 0 && item < list[i])
	{
		list[i+1] = list[i];
		i--;
	}
	list[i+1] = item;
	size++;
}
template<typename T, int length>
ostream& SortedList<T, length>::put(ostream& out) const
{
	for (int i = 0; i < size; i++)
		cout << list[i] << " " ;
	cout << endl;
	return out;
}
int main()
{
	int values[] = {5, 1, 7, 8, 11, 2};
	SortedList<int, 10> list;
	for (int i = 0; i < 6; i++)
		list.insert(values[i]);
	cout << list;
	return 0;
}



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).

1
2
3
4
5
6
7
8
private:
    ostream& put(ostream& out) const;
public:
    friend ostream& operator<<(ostream& out, const SortedList& list)
    {return list.put(out);}
    ...
private:
    ...

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.
Last edited on
The compiler shouldn't make a difference.
Have you tried making it public?
Also, post the exact error message.
Last edited on
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
Last edited on
What happens if you move the definition outside the class?
1
2
3
4
5
6
7
8
    //...
    friend ostream& operator<<(ostream& out, const SortedList& list);
    //...
};

ostream& operator<<(ostream& out, const SortedList& list){
     return list.put(out);
}

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);
}

Last edited on
I tried it and it said this
1>Exercise 4.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class SortedList<int,10> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$SortedList@H$09@@@Z) referenced in function _main
Interesting. Then it's definitely the compiler. Why are you using such an old compiler, anyway?
How can you tell what compiler that is just from the error? All I can tell is that it looks like msvc.
Last edited on
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.
Could you try one last experiment? What about this, a simplified version without the template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

class A {
private:
    int n;
    std::ostream& put(std::ostream& os) const;
public:
    A(int n) : n(n) {}
    friend std::ostream& operator<<(std::ostream& os, const A& a);
};

std::ostream& A::put(std::ostream& os) const {
    return os << n;
}

std::ostream& operator<<(std::ostream& os, const A& a) {
    return a.put(os);
}

int main() {
    A a(42);
    std::cout << a << '\n';
}

Last edited on
Ganado: OP has stated they're using MSVC 2010.
*facepalm* I see.
I've copied your code into a MSVC2010 project and built it.

however intellisense puts a red squiggle under "put" with the tooltip
Error: function "SortedList<T,length>::put" (declared at line 40) is inaccessible


is this what you're seeing?

on my machine it still builds fine with no errors, the warning from intellisense is a "fraud"
in your output window do you see this...
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========


ref: https://stackoverflow.com/questions/27966257/visual-studio-2010-c-compile-time-intellisense-errors
Last edited on
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.
Topic archived. No new replies allowed.