473,387 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Howto declare a friend function to a nested class

Dear all,

I'm trying to declare the operator++ to a nested class. The nested
class is not template but the container it is.

The code used in teh sample program is included bellow:
#include <iostream>
template <class T>
class A
{
public:
class B;
};
template <class T>
bool operator == <T(const A<T>::B &b1,
const class A<T>::B &b2);
template <class T>
class A<T>::B
{
public:
B(int a);

friend bool operator ==<(const A<T>::B &b1, const A<T>::B &b2);

private:
int aa;
B b;
};
template <class T>
A<T>::B::B(int a):
aa(a)
{}
template <class T>
bool operator == <>(const A<T>::B& b1, const A<T>::B& b2)
{
return b1.aa == b2.aa;
}
class C {};
int main()
{
A<Cac;

A<C>::B b1(4), b2(5);

bool res = (b1 == b2);
return 0;
}
I'm compiling with gcc version 4.1.2. The output of the compiler is
included bellow:

nested.cpp:13: error: expected initializer before '<' token
nested.cpp:23: error: expected unqualified-id before 'template'
nested.cpp:38: error: expected initializer before '<' token
nested.cpp: In instantiation of 'A<C>::B':
nested.cpp:51: instantiated from here
nested.cpp:27: error: 'A<T>::B::b' has incomplete type
nested.cpp:19: error: declaration of 'class A<C>::B'
nested.cpp: In function 'int main()':
nested.cpp:53: error: no match for 'operator==' in 'b1 == b2'

Any help will be appreciated.

Thanks and Best Regards,
Joaquim Duran

Sep 30 '07 #1
3 3862
jd*********@gmail.com wrote:
[..]
template <class T>
class A<T>::B
{
public:
B(int a);

friend bool operator ==<(const A<T>::B &b1, const A<T>::B &b2);

private:
int aa;
B b;
Am I reading this right? An instance of your 'B' class contains
another instance of the same class, 'b'? Not gonna work.

Fix this first, then post the corrected code again.

And keep in mind that a member of a class template _is_ essentially
a template itself.
};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 30 '07 #2
On 30 Set, 02:15, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
jduranco...@gmail.com wrote:
[..]
template <class T>
class A<T>::B
{
public:
B(int a);
friend bool operator ==<(const A<T>::B &b1, const A<T>::B &b2);
private:
int aa;
B b;

Am I reading this right? An instance of your 'B' class contains
another instance of the same class, 'b'? Not gonna work.

Fix this first, then post the corrected code again.

And keep in mind that a member of a class template _is_ essentially
a template itself.
};
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
I agree, in the sample, I've declared a recursive type that C++ is not
supporting. I've removed it, and I've improved the syntax (adding
typename as suggested, thanks). Now, the only error is the declaration
of a friend template function.
Improved source code:

#include <iostream>
template <typename T>
class A
{
public:
class B;
};
/* Example of template function */
template <typename T>
T add(T a, T b) {return a+b;}
template <typename T>
bool operator == (const typename A<T>::B &b1,
const typename A<T>::B &b2);
template <typename T>
class A<T>::B
{
public:
B(int a);

friend bool operator == <(const typename A<T>::B &b1,
const typename A<T>::B &b2); // Line 29

private:
int aa;
};
template <typename T>
A<T>::B::B(int a):
aa(a)
{}
template <typename T>
bool operator == (const typename A<T>::B& b1,
const typename A<T>::B& b2)
{
return b1.aa == b2.aa;
}
class C {};
int main()
{
A<C>::B b1(4), b2(5);

bool res = (b1 == b2); // Line 57
return 0;
}
The message error from compiler is:

nested.cpp: In instantiation of 'A<C>::B':
nested.cpp:55: instantiated from here
nested.cpp:29: error: template-id 'operator==<>' for 'bool
operator==(const A<C>::B&, const A<C>::B&)' does not match any
template declaration
nested.cpp: In function 'int main()':
nested.cpp:57: error: no match for 'operator==' in 'b1 == b2'

I've googled a litle abut how declare template functions as friends,
and It looks likes that the current syntax is right.

Thanks and Best Regads,
Joaquim Duran

Sep 30 '07 #3
jd*********@gmail.com wrote:
[...]
Improved source code:

#include <iostream>
template <typename T>
class A
{
public:
class B;
};
/* Example of template function */
template <typename T>
T add(T a, T b) {return a+b;}
template <typename T>
bool operator == (const typename A<T>::B &b1,
const typename A<T>::B &b2);
template <typename T>
class A<T>::B
{
public:
B(int a);

friend bool operator == <(const typename A<T>::B &b1,
Should be

friend bool operator == <T...
const typename A<T>::B &b2); // Line 29

private:
int aa;
};
template <typename T>
A<T>::B::B(int a):
aa(a)
{}
template <typename T>
bool operator == (const typename A<T>::B& b1,
const typename A<T>::B& b2)
{
return b1.aa == b2.aa;
}
class C {};
int main()
{
A<C>::B b1(4), b2(5);

bool res = (b1 == b2); // Line 57
There is no way for the compiler to determine that the template
operator should be used because from the expression 'b1 == b2'
the compiler cannot deduce the 'C' for the template -- the context
is not one of the deducible contexts.
return 0;
}
The message error from compiler is:

nested.cpp: In instantiation of 'A<C>::B':
nested.cpp:55: instantiated from here
nested.cpp:29: error: template-id 'operator==<>' for 'bool
operator==(const A<C>::B&, const A<C>::B&)' does not match any
template declaration
That can be rectified by placing 'T' in the angle brackets, see
above.
nested.cpp: In function 'int main()':
nested.cpp:57: error: no match for 'operator==' in 'b1 == b2'
This cannot be corrected because the compiler canno deduce the 'T'
for the template operator==
I've googled a litle abut how declare template functions as friends,
and It looks likes that the current syntax is right.
Apparently it wasn't.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kai Wu | last post by:
Hello, struct S{ friend class B; private: int i; }; class B{ B(){}
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
2
by: tinman | last post by:
Hi... I would like to achive the following object model in VB.NET.....not too sure how to achieve this but in VB6 I can declare the Project as PublicNotCreatable so as to prevent external...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
5
by: WittyGuy | last post by:
Hi all, All I want to achieve is restricting the object instantiation in stack and allowing the application to instantiate the object only in heap using new operator. How to achieve this? I...
7
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am running into with such a question when I tried to declare and define a friend template function in a template class, here is the code snippet: #include <iostream> using...
1
by: farseerfc | last post by:
Hi, everyone here loves C++; As a student studying data structure, I'm writting a single list (with only one pointer to identity the next node in its node struct) simulating std::list like: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.