Re: Problem with friend function and gcc 4.2 with
Re: Problem with friend function and gcc 4.2 with
- Subject: Re: Problem with friend function and gcc 4.2 with
- From: "Roni Music" <email@hidden>
- Date: Sat, 9 Aug 2008 06:42:21 +0200
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@hidden>
Subject: Re: Problem with friend function and gcc 4.2 with
objective-c++
To: cocoa-dev cocoa-dev <email@hidden>
Message-ID: <email@hidden>
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 (email@hidden)
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:
This email sent to email@hidden
--
Ken Worley
Software Engineer, Tiberius, Inc.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden