template friend operator generates "member is protected" error

Hi guys,

I have posted this in beginners because I am pretty sure the solution is simple but it so far has eluded me.

I have class template like so:
1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
class A
{
     public:
          template<class B>
          friend bool operator==(const B& lhs, const A<B>& rhs)
          {
               return (lhs == rhs.m_value);
          }
     protected:
          T m_value;
};

But when I try and use the operator I get the old "Error: A.m_value is protected".

Now I know what it means - but why is it coming up? Surely I should be able to access it from a friend operator?

Thank you for any assistance offered.

Phil.
Can you post your code that instantiates an object of class A and your code to invoke the operator==()?
Hi,

Here is the code where it is used, it is part of a unit test.

1
2
3
4
5
6
7
A<bool> vp_bool;
vp_bool = true;
if(false == vp_bool) {
     m_fail++;
     console("... failed.");
     return;
}

The "console" function is just a macro defined as:

 
#define console(x) std::cout << x << std::endl 

Class A is not part of a namespace and neither is the code that uses the it.

Thank you.

Phil.
When I compile your code I get an error with this statement: vp_bool = true; because you have not defined operator=() for A. Remember operator=() and operator==() are different.
Hi thank you for following this up - all operators have been defined but in the example that I posted I posted a cut down version as the actual class is a lot bigger. Here is the entire code for that class:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292

// decleration of A class for non-boolean types...
template< class T , bool = is_boolean< T >::value >
class A
{
	public:
		// construct, copy & destroy...
		A() : m_value(0) {};
		A(const T& value) : m_value(value) {};
		A(const A< T >& other) : m_value(other.m_value) {};
		virtual ~A() {};
		
		virtual operator T() 
		{
			return this->m_value;
		}
		
		virtual T& operator=(const T& value)
		{
			return (this->m_value = value);
		}
		
		// pointer operator - also not constant, as could alter class by calling a class method
		T* operator->()
		{
			T* ptr = &this->m_value;
			return ptr;
		}
		
		// equality operator - all implementations have this
		bool operator==(const T& value) const
		{
			return (this->m_value == value);
		}
		
		bool operator==(const A< T >& other) const
		{
			return (this->m_value == other.m_value);
		}
		
		template<class A>
		friend bool operator==(const A& value, const A< A >& other)
		{
			return (value == other.m_value);
		}
		
		// in-equality operator - all implementations have this
		bool operator!=(const T& value) const
		{
			return (this->m_value != value);
		}
		
		bool operator!=(const A< T >& other) const
		{
			return (this->m_value != other.m_value);
		}
		
		template<class B>
		friend bool operator!=(const B& value, const A< B >& other)
		{
			return (value != other.m_value);
		}
		
		// less-than operator - only non-boolean types have this
		bool operator<(const T& value) const
		{
			return (this->m_value < value);
		}
		
		bool operator<(const A< T , false >& other) const
		{
			return (this->m_value < other.m_value);
		}
		
		template<class B>
		friend bool operator<(const B& value, const B< A , false >& other)
		{
			return (value < other.m_value);
		}
		
		// greater-than operator - only non-boolean types have this
		bool operator>(const T& value) const
		{
			return (this->m_value > value);
		}
		
		bool operator>(const A< T , false >& other) const
		{
			return (this->m_value > other.m_value);
		}
		
		template<class B>
		friend bool operator>(const B& value, const A< B , false >& other)
		{
			return (value > other.m_value);
		}
		
		// less-than-or-equal-to operator - only non-boolean types have this
		bool operator<=(const T& value) const
		{
			return (this->m_value <= value);
		}
		
		bool operator<=(const A< T , false >& other) const
		{
			return (this->m_value <= other.m_value);
		}
		
		template<class B>
		friend bool operator<=(const B& value, const A< B , false >& other)
		{
			return (value <= other.m_value);
		}
		
		// greater-than-or-equal-to operator - only non-boolean types have this
		bool operator>=(const T& value) const
		{
			return (this->m_value >= value);
		}
		
		bool operator>=(const A< T , false >& other) const
		{
			return (this->m_value >= other.m_value);
		}
		
		template<class B>
		friend bool operator>=(const B& value, const A< B , false >& other)
		{
			return (value >= other.m_value);
		}
		
		// addition operator - only non-boolean types have this
		T operator+(const T& value) const
		{
			return (this->m_value + value);
		}
		
		T operator+(const A< T , false >& other) const
		{
			return (this->m_value + other.m_value);
		}
		
		template<class B>
		friend B operator+(const B& value, const A< B , false >& other)
		{
			return (value + other.m_value);
		}
		
		// subtraction operator - only non-boolean types have this
		T operator-(const T& value) const
		{
			return (this->m_value - value);
		}
		
		T operator-(const A< T , false >& other) const
		{
			return (this->m_value - other.m_value);
		}
		
		template<class B>
		friend B operator-(const B& value, const A< B , false >& other)
		{
			return (value - other.m_value);
		}
		
		// multiplication operator - only non-boolean types have this
		T operator*(const T& value) const
		{
			return (this->m_value * value);
		}
		
		T operator*(const A< T , false >& other) const
		{
			return (this->m_value * other.m_value);
		}
		
		template<class B>
		friend B operator*(const B& value, const A< B , false >& other)
		{
			return (value * other.m_value);
		}
		
		// division operator - only non-boolean types have this
		T operator/(const T& value) const
		{
			return (this->m_value / value);
		}
		
		T operator/(const A< T , false >& other) const
		{
			return (this->m_value / other.m_value);
		}
		
		template<class B>
		friend B operator/(const B& value, const A< B , false >& other)
		{
			return (value / other.m_value);
		}
		
		// modulus operator - only non-boolean types have this
		T operator%(const T& value) const
		{
			return (this->m_value % value);
		}
		
		T operator%(const A< T , false >& other) const
		{
			return (this->m_value % other.m_value);
		}
		
		template<class B>
		friend B operator%(const B& value, const A< B , false >& other)
		{
			return (value % other.m_value);
		}
		
	protected:
		// members
		T m_value;
};

// specialization for boolean types...
template<>
class A< bool , true >
{
	public:
		// construct, copy & destroy...
		A() : m_value(false) {};
		A(const bool& value) : m_value(value) {};
		A(const A< bool , true >& other) : m_value(other.m_value) {};
		virtual ~A() {};
	
		virtual operator bool()
		{
			return this->m_value;
		}
		
		virtual bool& operator=(const bool& value)
		{
			return (this->m_value = value);
		}
		
		// pointer operator
		bool* operator->()
		{
			bool* ptr = &this->m_value;
			return ptr;
		}
		
		// equality operator
		bool operator==(const bool& other) const
		{
			return (this->m_value == other);
		}
		
		bool operator==(const A< bool , true >& other) const
		{
			return (this->m_value == other.m_value);
		}
		
		// in-equality operator
		bool operator!=(const bool& other) const
		{
			return (this->m_value != other);
		}
		
		bool operator!=(const A< bool , true >& other) const
		{
			return (this->m_value != other.m_value);
		}
		
		// boolean only operators
		bool operator!() const
		{
			return (!this->m_value);
		}
		
		bool operator&&(const bool& value) const
		{
			return (this->m_value && value);
		}
		
		bool operator||(const bool& value) const
		{
			return (this->m_value || value);
		}
		
	protected:
		// members
		bool m_value;
};


And here is the definition of the is_boolean<T> struct:

1
2
template<typename T> struct is_boolean		{ const static bool value = false; };
template<> struct is_boolean<bool>		{ const static bool value = true; };


That's everything - the body of the unit test that the compiler doesn't like is posted in my previous post. I hope this helps.

Thank you,

Phil.
Think about template instantiation.
1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
class A
{
     public:
          template<class B>
          friend bool operator==(const B& lhs, const A<B>& rhs)
          {
               return (lhs == rhs.m_value);
          }
     protected:
          T m_value;
};

==() is a friend to A<T>, but not a friend to A<B>.
A<T> and A<B> are two different instances of Template A.
If you want ==() to be a friend of the template instead of a specific instance of that template, maybe you should write like this.
1
2
3
public:
friend bool operator == ( const T& lhs, const A & rhs)
{}
Good Luck:)

I am getting different compile errors to you.

You seem to be using the name A for everything here, the main class as well as the template parameter....
1
2
3
4
5
		template<class A>
		friend bool operator==(const A& value, const A< A >& other)
		{
			return (value == other.m_value);
		}

Did you mean something like this?
1
2
3
4
5
		template<class X>
		friend bool operator==(const X& value, const A< X >& other)
		{
			return (value == other.m_value);
		}


I wonder if you might be confusing your template parameter with your class here...
1
2
3
4
5
		template<class B>
		friend bool operator<(const B& value, const B< A , false >& other)
		{
			return (value < other.m_value);
		}

Did you mean something like this?
1
2
3
4
5
		template<class B>
		friend bool operator<(const B& value, const A< B , false >& other)
		{
			return (value < other.m_value);
		}

Hi,

Thank you both for the responses. I don't know why the compiler was not picking up those problems for me (where I got the templates names the wrong way around) - but I appreciate you pointing them out to me. I was deliberately using the explicit template operators because I didn't want the operators for certain types - however, it does seem that I have applied that to methods that should belong to the template class.

Thank you for taking the time to help me on this.

Kind Regards,

Phil.
Topic archived. No new replies allowed.