Operator signatures are already know by the compiler since they are defined in the standard, and are really global in scope. Thus, you don't have to declare them prior to defining them.

Jonathan

On Aug 10, 2008, at 11:09 PM, Ken Worley wrote:

First, I appreciate the response and discussion. Thank you. My response is not meant to be argumentative, but I'd like to get to the bottom of this.

The main thrust of that discussion (on bytes.com) seems to be that since the friend function is both declared and defined inline within the class test1 declaration, that it is trapped in test1's namespace and only usable from within the class. At least, that's the way I understand the discussion.

FWIW, using gcc 4.2, and it turns out it doesn't matter if I'm compiling the file as C++ or objective-C++, the following both fail with the same compile error:

1. Trying to call newtest1 from within a method of test1

2. Calling the friend function like this: test1* newobj = test1::newtest1(5); from within main.

So, in what circumstance _would_ I be able to call that friend function? It's not usable from within another method of that class and it is apparently not callable from outside the class without duplicating the function prototype outside the body of the class. Duplicating the prototype outside the class declaration DOES allow the example to compile (whether it's added before or after the class declaration).

So, is there some exception for operator friend functions? I can add an operator friend function declared and defined inline within the body of the class declaration (just like the newtest1 friend function). That type of friend function compiles and works just fine and is callable from main...

So I'm confused. The compiler allows the friend function to be declared and defined inline within the class declaration, but I'm not allowed to call it from anywhere without another declaration of the prototype elsewhere. That doesn't seem right to me, but I'll concede that it might be. If that's the case, then there must an exception for friend operators or that's another bug in the compiler except this one's not fixed in gcc 4.2.

Ken

On Aug 10, 2008, at 2:13 PM, Roni Music wrote:


You can read a discussion regarding this problem here:
http://bytes.com/forum/thread828536.html

it seems gcc 4.0 had a bug which is corrected in gcc 4.2


Message: 12
Date: Sat, 9 Aug 2008 09:36:17 -0600
From: Ken Worley <[EMAIL PROTECTED]>
Subject: Re: Problem with friend function and gcc 4.2 with
To: cocoa-dev cocoa-dev <cocoa-dev@lists.apple.com>
Cc: Roni Music <[EMAIL PROTECTED]>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes

FWIW, it does in fact compile in 4.2 (and in 4.0) when formatted as
you suggest. I still believe the original format is correct and that
4.2 is the version with the bug. The info Clark Cox pointed out seems
to support that. In any case, a bug has been filed, so I'll know
Apple's point of view eventually :) Thanks for the input (and thanks
also to Clark and Thomas for responding).

rdar://6135771

Ken

On Aug 8, 2008, at 10:42 PM, Roni Music wrote:


I'm not a C++ expert but your code below should not compile (as I
see it)

You should declare the friend function inside the class:

class test1
{
public:

friend test1* newtest1(int x);
/*
the function newtest1() is now a friend to test1 class and may
access private member variables
and functions such as finishinit()
*/

snip....
};

then define the function outside the class:

test1* newtest1(int x)
{
test1* anobj = new test1();
anobj->finishinit(x);
return anobj;
}

So if you code worked with gcc 4.0 and not gcc 4.2, then it seems
gcc 4.2 now works according to the C++ standard
and gcc 4.0 did not

Rolf




Message: 10
Date: Fri, 8 Aug 2008 16:41:42 -0600
From: Ken Worley <[EMAIL PROTECTED]>
Subject: Re: Problem with friend function and gcc 4.2 with
objective-c++
To: cocoa-dev cocoa-dev <cocoa-dev@lists.apple.com>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
delsp=yes

Hmmm, no response as of yet. I went ahead and submitted a bug against
Xcode: rdar://6135771
We'll see what happens.

Ken
On Aug 7, 2008, at 4:09 PM, Ken Worley wrote:

Hi all,

I'm using Xcode 3.1 and just switched to gcc 4.2 from 4.0, but I've
run into a problem with friend functions when compiling in
objective-
c++. I contrived an example that illustrates the problem:

1. Created new Cocoa project
2. Forced compilation of all files to use objective-c++
3. Changed content of main.m to below...

This project builds fine using gcc 4.0, but when I switch the
compiler setting to use gcc 4.2, I get the errors listed below. Any clues would certainly be appreciated if I'm doing something wrong.
If not, I guess I'll file a bug...

Here's main.m:

#import <Cocoa/Cocoa.h>

class test1
{
public:

friend test1* newtest1(int x)
{
test1* anobj = new test1();
anobj->finishinit(x);
return anobj;
}

virtual ~test1()
{
}

private:

int avalue;

test1()
{
avalue = 0;
}

void finishinit(int x)
{
avalue = x;
}
};

int main(int argc, char *argv[])
{
test1* tobj = newtest1(5);
delete tobj;

return NSApplicationMain(argc,  (const char **) argv);
}


Here's the build log:

Building target "Untitled" of project "Untitled" with configuration
"Debug" - (1 error)
 cd /Users/ken/Desktop/Untitled
/Xcode3.1/Developer/usr/bin/gcc-4.2 -x objective-c++ -arch i386 -
fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm- blocks -O0 -Wreturn-type -Wunused-variable -isysroot /Xcode3.1/ Developer/ SDKs/MacOSX10.5.sdk -mfix-and-continue -fvisibility-inlines- hidden -
mmacosx-version-min=10.5 -gdwarf-2 -iquote /Users/ken/Desktop/
Untitled/build/Untitled.build/Debug/Untitled.build/Untitled-
generated-files.hmap -I/Users/ken/Desktop/Untitled/build/
Untitled.build/Debug/Untitled.build/Untitled-own-target- headers.hmap
-I/Users/ken/Desktop/Untitled/build/Untitled.build/Debug/
Untitled.build/Untitled-all-target-headers.hmap -iquote /Users/ ken/ Desktop/Untitled/build/Untitled.build/Debug/Untitled.build/ Untitled- project-headers.hmap -F/Users/ken/Desktop/Untitled/build/Debug - I/ Users/ken/Desktop/Untitled/build/Debug/include -I/Users/ken/ Desktop/ Untitled/build/Untitled.build/Debug/Untitled.build/ DerivedSources -
include /var/folders/JE/JEJ3RSLHE9uIDGjXTRTis++++TI/-Caches-/
com.apple.Xcode.501/SharedPrecompiledHeaders/Untitled_Prefix-
brblicjbwwpqhfahflncgqpvarno/Untitled_Prefix.pch -c /Users/ken/
Desktop/Untitled/main.m -o /Users/ken/Desktop/Untitled/build/
Untitled.build/Debug/Untitled.build/Objects-normal/i386/main.o
/Users/ken/Desktop/Untitled/main.m: In function 'int main(int,
char**)':
/Users/ken/Desktop/Untitled/main.m:43: error: 'newtest1' was not
declared in this scope
/Users/ken/Desktop/Untitled/main.m:43: error: 'newtest1' was not
declared in this scope
Build failed (1 error)

Thanks,
Ken

--
Ken Worley
Software Engineer, Tiberius, Inc.



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/cocoadev%40tiberiusinc.com

This email sent to [EMAIL PROTECTED]

--
Ken Worley
Software Engineer, Tiberius, Inc.






--
Ken Worley
Software Engineer, Tiberius, Inc.





------------------------------

_______________________________________________

Cocoa-dev mailing list      (Cocoa-dev@lists.apple.com)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins (at) lists.apple.com

http://lists.apple.com/mailman/listinfo/cocoa-dev


End of Cocoa-dev Digest, Vol 5, Issue 1422
******************************************


--
Ken Worley
Software Engineer, Tiberius, Inc.



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/jprescott12%40comcast.net

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to