473,748 Members | 9,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

conflict between friend function and inherited class function

Dear all, once again I stumbled upon the following puzzling problem.

When having the following two files (see below), the gnu compiler
compiles the file without a problem while the compiler complains about
the fact that the function foo (which is declared as a template) is not
a template! The problem itself vanishes when changing the name of the
function foo in the class bar into some other function name, lets say goo.

The problem apears to originate from the same function name which excist
in the parent class.

So my question is now, is this syntax correct, or is one of the
compilers failing?

Thanks for the help
[ testing]$ g++ -c car.cpp
[ testing]$ icpc -c car.cpp
vector.hpp(13): error: foo is not a template
friend void foo <(car<T&, car<T&);
^
detected during instantiation of class "car<T[with T=int]"
at line 2 of "car.cpp"

compilation aborted for vector.cpp (code 2)
======== car.hpp ========
#ifndef CAR_HPP
#define CAR_HPP

template<typena me Tclass bar {
public :
void foo(bar<T&); // not working
// void goo(bar<T&); THIS ONE WOULD WORK
};

template<typena me T class car;
template<typena me Tvoid foo(car<T&, car<T&);

template<typena me Tclass car : public bar<T{
friend void foo <(car<T&, car<T&);
};
#endif
============= car.cpp =============
#include "car.hpp"
template class car<int>;
=============== =============== =====
Jan 17 '08 #1
4 2296
ciccio wrote:
Dear all, once again I stumbled upon the following puzzling problem.

When having the following two files (see below), the gnu compiler
compiles the file without a problem while the compiler
Sorry, which one?
complains about
the fact that the function foo (which is declared as a template) is
not
a template! The problem itself vanishes when changing the name of the
function foo in the class bar into some other function name, lets say
goo.

The problem apears to originate from the same function name which
excist in the parent class.

So my question is now, is this syntax correct, or is one of the
compilers failing?
They both can be failing.
>
Thanks for the help
[ testing]$ g++ -c car.cpp
To verify your code better, you need to make the compilation _strict_
and _conforming_. Here you're just letting GNU extensions loose.
[ testing]$ icpc -c car.cpp
vector.hpp(13): error: foo is not a template
friend void foo <(car<T&, car<T&);
^
detected during instantiation of class "car<T[with T=int]"
at line 2 of "car.cpp"

compilation aborted for vector.cpp (code 2)
======== car.hpp ========
#ifndef CAR_HPP
#define CAR_HPP

template<typena me Tclass bar {
public :
void foo(bar<T&); // not working
// void goo(bar<T&); THIS ONE WOULD WORK
};

template<typena me T class car;
template<typena me Tvoid foo(car<T&, car<T&);

template<typena me Tclass car : public bar<T{
friend void foo <(car<T&, car<T&);
If youi need a particular instantiation of 'foo' to be the friend,
you need to give it the template arguments, I believe

friend void foo<T>(car<T>&, car<T>&);

Have you tried that?
};
#endif
============= car.cpp =============
#include "car.hpp"
template class car<int>;
=============== =============== =====
So, have you actually tried inserting the entire 'car.hpp' into
'car.cpp' and then posting it here. It doesn't matter really that
you have two files, does it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 17 '08 #2
<snip>
To verify your code better, you need to make the compilation _strict_
and _conforming_. Here you're just letting GNU extensions loose.
What do you mean with gnu extensions? Could you elaborate a bit on this?
If youi need a particular instantiation of 'foo' to be the friend,
you need to give it the template arguments, I believe

friend void foo<T>(car<T>&, car<T>&);

Have you tried that?
Yep, it gives the same error. And the above rule is given in faq-lite
also. When I remove the <from the friend declaration, i.e.
friend void foo(car <T&, car<T&);
The intel compiler gives a warning saying that I might need to include
the <.
So, have you actually tried inserting the entire 'car.hpp' into
'car.cpp' and then posting it here. It doesn't matter really that
you have two files, does it?
Sorry, you are right there, here is the one file example

=== START CAR.CPP ======
template<typena me Tclass bar {
public :
void foo(bar<T&);
};

template<typena me T class car;
template<typena me Tvoid foo(car<T&, car<T&);

template<typena me Tclass car : public bar<T{
friend void foo <T(car<T&, car<T&);
};

car<inta;

===== END CAR.CPP =====

Again the same question remains, why does foo from the class bar
conflicts with the friend definition of the template function foo in the
class car?
Jan 17 '08 #3
ciccio wrote:
<snip>
>To verify your code better, you need to make the compilation _strict_
and _conforming_. Here you're just letting GNU extensions loose.

What do you mean with gnu extensions? Could you elaborate a bit on
this?
Not sure what to tell you. GNU C++ is a slightly different language
than standard C++. It's full of extensions, some of which are bugs
in the compiler or in the compiler creators' understanding of the
Standard. You need to disable those. See man page to find out what
command-line switches to use.
>
>If youi need a particular instantiation of 'foo' to be the friend,
you need to give it the template arguments, I believe

friend void foo<T>(car<T>&, car<T>&);

Have you tried that?

Yep, it gives the same error. And the above rule is given in faq-lite
also. When I remove the <from the friend declaration, i.e.
friend void foo(car <T&, car<T&);
The intel compiler gives a warning saying that I might need to include
the <.
>So, have you actually tried inserting the entire 'car.hpp' into
'car.cpp' and then posting it here. It doesn't matter really that
you have two files, does it?

Sorry, you are right there, here is the one file example

=== START CAR.CPP ======
template<typena me Tclass bar {
public :
void foo(bar<T&);
};

template<typena me T class car;
template<typena me Tvoid foo(car<T&, car<T&);

template<typena me Tclass car : public bar<T{
friend void foo <T(car<T&, car<T&);
};

car<inta;

===== END CAR.CPP =====

Again the same question remains, why does foo from the class bar
conflicts with the friend definition of the template function foo in
the class car?
The code you posted here compiles without a problem with Comeau online.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 17 '08 #4
>What do you mean with gnu extensions? Could you elaborate a bit on
>this?

Not sure what to tell you. GNU C++ is a slightly different language
than standard C++. It's full of extensions, some of which are bugs
in the compiler or in the compiler creators' understanding of the
Standard. You need to disable those. See man page to find out what
command-line switches to use.
I have switched those extensions off and the code compiles still fine in
GNU C++. I have done the same with the intel compiler but there it
gives that error again. Nonetheless the intel compiler has an other
option called -strict-ansi, with this it is possible to compile, but the
code becomes much slower due to this and is not really acceptable.

And since the intel compiler bases itself on the GNU compiler, I am a
bit surprised by its error.
Jan 18 '08 #5

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

Similar topics

15
6589
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
7
9820
by: Jesper | last post by:
I need to grant a class access to protected fields of another class in the way its possible in C++ with the friend keyword. However I would like to keep the class protected towards other class within the same program/assembly. The two classes are not 'related' (inherited). How can I do this. I cant see how the keyword Internal can be used for this purpose.
2
13014
by: K. Shier | last post by:
Friend variables "are accessible from within the program that contains their declaration and from anywhere else in the same assembly." according to the VS.Net help. This sounds like a pretty broad scope! So why, when attempting to inherit a form from a base form class, is 'Friend' inadequate to give inherited forms 'public'-style access to inherited controls? I.E. - i have a base form with two group boxes on it. When they are declared...
2
1560
by: Vark | last post by:
Interesting behavior I came across today where a child form has full access to a base form class' controls, but you can only modify the controls' properties in code, not in the visual designer. Here's how you can reproduce it: New Winforms project On Form1, drop a visual control. I'll use a panel in this example. Make sure Panel1 modifier is set to "Friend" Build project Add another form - this one an inherited form. Set it to inherit...
8
1482
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements) IParser, one for XML, ne for Config, & one for binary. They need to get the Document & CC class and fill the data, in the
6
5554
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1, next2; };
3
1733
by: Neal | last post by:
Hi all Why do I get a compiler error with the following code. Friend Class SomeClass End Class Public Class SomePublicClass Protected Friend Function AMethod As SomeClass
3
1736
by: tonvandenheuvel | last post by:
Hi all, the following code does not compile in gcc 4.0.2: 3 class Outer 4 { 5 class B; 6 7 template<typename T>
8
3726
boxfish
by: boxfish | last post by:
Hi everyone, I'm working on a 3d maze game, and I just messed up the classes so it won't compile, and I need help sorting it out. There's a class Maze, whose members are, among other things, a list of the walls, the user who is an object of class MazePerson, and a yellow square who is an object of class MazeMonster. MazePerson and MazeMonster are both inherited from class MazeThing, which takes care of stuff like collisions. MazePerson,...
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.