473,804 Members | 2,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function declared before call but defined after - compilation fails

Hello All,

I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:

//: C10:FriendInjec tion.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};
}
// 1
void Me::you(){}

int main() {
Me::you();
return 0;
}

// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////

If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: 'you' is not a member of 'Me'". If, instead, Me::you is
*defined* at (1),
then it compiles.

MSVC++ Express compiles it either way without a problem, as expected.

Surely this is a bug in gcc?

May 16 '07 #1
11 2029

<jo**********@h otmail.comwrote in message
news:11******** *************@q 23g2000hsg.goog legroups.com...
Hello All,

I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:

//: C10:FriendInjec tion.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};
}
// 1
void Me::you(){}

int main() {
Me::you();
return 0;
}

// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////

If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: 'you' is not a member of 'Me'". If, instead, Me::you is
*defined* at (1),
then it compiles.

MSVC++ Express compiles it either way without a problem, as expected.

Surely this is a bug in gcc?
Why?

Declaring a function as a friend does not declare or define that function.
It merely states that said function is a friend.

In case (1), you're declaring *and* defining Me::you(). If you move that to
after main() as in case (2), then when main() is compiled, Me::you() has not
yet been declaredand so it's an error.

-Howard

May 16 '07 #2
JLS
On May 16, 11:38 am, johnbrown...@ho tmail.com wrote:
Hello All,

I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully, but please consider the following program:

//: C10:FriendInjec tion.cpp
// From Thinking in C++, 2nd Edition
// Available athttp://www.BruceEckel. com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};}

// 1
void Me::you(){}

int main() {
Me::you();
return 0;

}

// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////

If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: 'you' is not a member of 'Me'". If, instead, Me::you is
*defined* at (1),
then it compiles.

MSVC++ Express compiles it either way without a problem, as expected.

Surely this is a bug in gcc?
What happens if you change it to

int main() {
Me::Us::you();
return 0;

I would have thought that the "Us" would be necessary.

May 16 '07 #3

"JLS" <de********@yah oo.comwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.com.. .
On May 16, 11:38 am, johnbrown...@ho tmail.com wrote:
>Hello All,

I am doing another exercise (I repeat, *exercise*). The (irrelevant to
this
discussion) point is to show that "You can inject a friend declaration
into
a namespace by declaring it within an enclosed class". I have done
this
successfully , but please consider the following program:

//: C10:FriendInjec tion.cpp
// From Thinking in C++, 2nd Edition
// Available athttp://www.BruceEckel. com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};}

// 1
void Me::you(){}

int main() {
Me::you();
return 0;

}

// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////

If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: 'you' is not a member of 'Me'". If, instead, Me::you is
*defined* at (1),
then it compiles.

MSVC++ Express compiles it either way without a problem, as expected.

Surely this is a bug in gcc?

What happens if you change it to

int main() {
Me::Us::you();
return 0;

I would have thought that the "Us" would be necessary.
Huh? The function you() is not a member of Us. It's a *friend* of Us.

-Howard

May 16 '07 #4
Surely this is a bug in gcc?

gcc 4.0.1 compiles it either way for me.

May 16 '07 #5
On May 16, 11:16 am, "Howard" <alic...@hotmai l.comwrote:
<johnbrown...@h otmail.comwrote in message

Why?

Declaring a function as a friend does not declare or define that function.
It merely states that said function is a friend.

In case (1), you're declaring *and* defining Me::you(). If you move that to
after main() as in case (2), then when main() is compiled, Me::you() has not
yet been declared and so it's an error.

-Howard
Bruce Eckel has this to say in his book:

"Notice the two other friend functions. The first declares an ordinary
global function g( ) as a friend. But g( ) has not been previously
declared at the global scope! It turns out that friend can be used
this way to simultaneously declare the function *and* give it friend
status."

Of course, he could be wrong, but then again, so could you. I have not
read the standards myself, and I probably never will. Who agrees with
Bruce? With Howard?

May 16 '07 #6
On May 16, 5:38 pm, johnbrown...@ho tmail.com wrote:
I am doing another exercise (I repeat, *exercise*). The
(irrelevant to this discussion) point is to show that "You can
inject a friend declaration into a namespace by declaring it
within an enclosed class".
This is not up to date. Originally, a name declared in a friend
declaration was injected into the enclosing namespace (or file
scope, as it was then). This was changed in the standard, and
friend names are no longer injected.
I have done this successfully, but
please consider the following program:
//: C10:FriendInjec tion.cpp
// From Thinking in C++, 2nd Edition
// Available athttp://www.BruceEckel. com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
namespace Me {
class Us {
//...
public:
friend void you();
};}
// 1
void Me::you(){}
int main() {
Me::you();
return 0;

}
This should not compile with an up-to-date compiler. Bruce
Eckel doubtlessly learned C++ before the change:-).

This would be legal according to the ARM, if you dropped the
namespace. I suspect that there were also many compilers which
supported it when they first introduced namespace as well.
// 2
// defining you() after main() does not compile
// void Me::you(){}
///:~
/////////////////////////////////////////////////////////////////////////
If Me::you() is *defined* at (2) after the call in main(), gcc 4.1.1
says:
"error: 'you' is not a member of 'Me'". If, instead, Me::you is
*defined* at (1),
then it compiles.
MSVC++ Express compiles it either way without a problem, as expected.
Surely this is a bug in gcc?
I wouldn't call it a bug. Technically, g++ is correct,
according to the standard. MSV++ is correct, according to
pre-standard conventions.

In practice, it's hard to conceive of a real program where the
change makes a difference. The most typical use of the friend
function is for operator's, which have a parameter of the class
type, and in this case, ADL now picks up the name. (ADL wasn't
present in earlier C++.) FWIW: this change broke none of my
earlier C++ (although other changes did).

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 16 '07 #7
On May 16, 12:22 pm, BigBrian <w...@brianmiel ke.comwrote:
Surely this is a bug in gcc?

gcc 4.0.1 compiles it either way for me.
As does gcc-3.4.2, OpenWatcom 1.6 and DigitalMars 8.49. For what it's
worth, the Comeau compiler (with default options) at
http://www.comeaucomputing.com/tryitout/ likes neither (1) nor (2).

So Howard, does your compiler reject (2), as you say it should?

May 16 '07 #8

<jo**********@h otmail.comwrote in message
news:11******** **************@ n59g2000hsh.goo glegroups.com.. .
On May 16, 12:22 pm, BigBrian <w...@brianmiel ke.comwrote:
Surely this is a bug in gcc?

gcc 4.0.1 compiles it either way for me.

As does gcc-3.4.2, OpenWatcom 1.6 and DigitalMars 8.49. For what it's
worth, the Comeau compiler (with default options) at
http://www.comeaucomputing.com/tryitout/ likes neither (1) nor (2).

So Howard, does your compiler reject (2), as you say it should?
Nope. VC++ (from Visual Studio 2003) accepts it either way. But the
question is what does the standard say, not what does Microsoft (or any
other vendor) say. I don't have the standard handy (and I have a great deal
of trouble understanding its legalese style), but I think James Kanze's
answer covers it pretty well.

-Howard

May 16 '07 #9
On May 16, 3:39 pm, James Kanze <james.ka...@gm ail.comwrote:
On May 16, 5:38 pm, johnbrown...@ho tmail.com wrote:
I am doing another exercise (I repeat, *exercise*). The
(irrelevant to this discussion) point is to show that "You can
inject a friend declaration into a namespace by declaring it
within an enclosed class".

This is not up to date. Originally, a name declared in a friend
declaration was injected into the enclosing namespace (or file
scope, as it was then). This was changed in the standard, and
friend names are no longer injected.
I see. Well, the book is old ((c) 2000) and the compilers mentioned in
my later post are also old (mostly).
>
<cut>

I wouldn't call it a bug. Technically, g++ is correct,
according to the standard. MSV++ is correct, according to
pre-standard conventions.
<cut>
The most typical use of the friend
function is for operator's, which have a parameter of the class
type, and in this case, ADL now picks up the name. (ADL wasn't
present in earlier C++.)
Is this "ADL" why the mighty Comeau compiler accepts neither (1) nor
(2)?

May 17 '07 #10

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

Similar topics

6
3171
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
2
1486
by: nospam_timur | last post by:
I'm writing a Linux device driver that needs to compile with several different Linux versions. In my code, I need to reference certain functions by their address alone. Something like this: int myfunc(char *x); if (memory_test == myfunc) .... In other words, I don't care about the return values or the parameters of myfunc(), I just need to reference it.
6
4413
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the same invocation from a DistantlyRelated class. What actually happened was that the compiler produced: error C2247: 'A::function' not accessible because 'CloselyRelated' uses 'private' to inherit from 'A' I'm guessing that the above compiler...
10
24090
by: bienwell | last post by:
Hi, I have a question about file included in ASP.NET. I have a file that includes all the Sub functions (e.g FileFunct.vb). One of the functions in this file is : Sub TestFunct(ByVal strInput As String) return (strInput & " test") End Sub
24
1340
by: junky_fellow | last post by:
Hi, Is there any way, by which we can limit a specific function to be called only from a specific function ? I dont know the advantage of this. Someone asked this question from me in an interview. thanks for any help ...
28
4720
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
17
2480
by: Jason Doucette | last post by:
I am converting a C-style unit into a C++ class. I have an implementation function that was defined in the .cpp file (so it was hidden from the interface that exists in the .h file). It uses a structure that is only needed by the implementation, so it were declared in the .cpp file, as well. Now, when converting this into a class, the class definition exists in the .h file, since it's required by the interface. The implementation...
12
2312
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and switch statements. My first approach was to rely on partial template specialization. Therefore, I have: // .h file template <int vSomeClass;
6
1461
by: Rahul | last post by:
Hi Everyone, when a call to a function is done in a file and if the function isn't defined, compiler just assumes that it would return a int and that the definition would be available at some other compilation unit which should be given to the linker to generate the executable file. However, why doesn't this extend to a global variable which isn't declared? The compiler gives as error immediately...
0
9704
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
10319
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
9132
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...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
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
5508
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...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
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.