error gcc 4 and friend class

M

maxime_phan

Hello, I have a big problem porting a huge c++ application on MacOS X
and gcc 4
here is my problem:
I have a code look like that in a header:

#if(defined(NOMCLASS))

class NOMCLASS {
.... //some code
....
friend class MACRO1(TOTO); //MACRO is a macro define somewhere
friend class MACRO1(TATA);
}

#endif

unfortunately, there is sometime where MACRO1(TOTO) is replaces by the
same name than NOMCLASS, fo exemple c_TOTO i have then:
class c_TOTO {
.... //some code
....
friend class c_TOTO;
....

that was no problem under visual studio, but in that case, i have the
following error: c_TOTO implicitly friends with itself.

how can I remove that error? I cannot change the code, it's huge and
occur many time. but i try to find some trick like if NOMCLASS equal
to MACRO1(TOTO); then don't compile the line friend class c_TOTO; but
it's preprocessor token and macro i don't think i can "compare"...

anybody have idea to help me about that?
thanks in advance.
 
V

Vladimir Jovic

Hello, I have a big problem porting a huge c++ application on MacOS X
and gcc 4
here is my problem:
I have a code look like that in a header:

#if(defined(NOMCLASS))

class NOMCLASS {
... //some code
...
friend class MACRO1(TOTO); //MACRO is a macro define somewhere
friend class MACRO1(TATA);
}

#endif

unfortunately, there is sometime where MACRO1(TOTO) is replaces by the
same name than NOMCLASS, fo exemple c_TOTO i have then:
class c_TOTO {
... //some code
...
friend class c_TOTO;
...

that was no problem under visual studio, but in that case, i have the
following error: c_TOTO implicitly friends with itself.

how can I remove that error? I cannot change the code, it's huge and
occur many time. but i try to find some trick like if NOMCLASS equal
to MACRO1(TOTO); then don't compile the line friend class c_TOTO; but
it's preprocessor token and macro i don't think i can "compare"...

anybody have idea to help me about that?

1) Do not use macros
2) See http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
3) What are MACRO1 and TATA??
 
T

tonydee

Hello, I have a big problem porting a huge c++ application on MacOS X
and gcc 4
here is my problem:
I have a code look like that in a header:

#if(defined(NOMCLASS))

class NOMCLASS {
... //some code
...
friend class MACRO1(TOTO); //MACRO is a macro define somewhere
friend class MACRO1(TATA);

}

#endif

unfortunately, there is sometime where MACRO1(TOTO) is replaces by the
same name than NOMCLASS, fo exemple c_TOTO i have then:
class c_TOTO {
... //some code
...
friend class c_TOTO;
...

that was no problem under visual studio, but in that case, i have the
following error: c_TOTO implicitly friends with itself.

how can I remove that error? I cannot change the code, it's huge and
occur many time. but i try to find some trick like if NOMCLASS equal
to MACRO1(TOTO); then don't compile the line friend class c_TOTO; but
it's preprocessor token and macro i don't think i can "compare"...

anybody have idea to help me about that?
thanks in advance.

I can't think of any trick. I think it's more likely you'll think of
a trick to change the code... maybe a regular expression based
substitution...? Wrap the troublesome statements in a #if TOTO !=
NOMCLASS / #endif block.

Cheers,
Tony
 
M

maxime_phan

hi,
MACRO1 and TATA are macro that returns name for class.
I have no choice using that macro, the architecture of the application
is like that

in the company i am, c++ templates are forbiden, so i guess this
system macro is done to replace them, but under g++ 4, it makes a lot
of trouble because of error "explicitly friends with itself", no
problem with visual studio 2008, maybe there a g++ compiler option to
resolve that? (-fpermissive not allowed)

thanks
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello, I have a big problem porting a huge c++ application on MacOS X
and gcc 4
here is my problem:
I have a code look like that in a header:

#if(defined(NOMCLASS))

class NOMCLASS {
... //some code
...
friend class MACRO1(TOTO); //MACRO is a macro define somewhere
friend class MACRO1(TATA);
}

#endif


And you write else-thread that
in the company i am, c++ templates are forbiden, so i guess this
system macro is done to replace them


This is just so braindead that it /hurts/ to read about it. You really have my
sympathy working there! But you're looking for a technical solution to
unfortunately, there is sometime where MACRO1(TOTO) is replaces by the
same name than NOMCLASS, fo exemple c_TOTO i have then:
class c_TOTO {
... //some code
...
friend class c_TOTO;
...


I don't know of any good solution, but I suggest that if it's practically
possible you replace C++ friendship with inheritance from a common base class.

The details of that will depend on your concrete classes.

that was no problem under visual studio, but in that case, i have the
following error: c_TOTO implicitly friends with itself.

how can I remove that error? I cannot change the code, it's huge and
occur many time. but i try to find some trick like if NOMCLASS equal
to MACRO1(TOTO); then don't compile the line friend class c_TOTO; but
it's preprocessor token and macro i don't think i can "compare"...

anybody have idea to help me about that?
thanks in advance.


Cheers & hth.,

- Alf
 
M

maxime_phan

i think they decide to forbid templates because the code must compile
and run on many kind os OS
i think inheritance from a common base class can be the solution

Thanks for your help!
 
A

Alf P. Steinbach

* (e-mail address removed):
unfortunately it seems frienship between class cannot be inherited :-(

Yes.

The idea was to put the functionality that friendship currently provides access
to, in a common base class. To /replace/ friendship based access with access to
base class functionality. In the base class it can all be made 'protected'.

I'm not sure how practical that is in classes that emulate templatizing via
macros, because likely there will be e.g. a 'double' in one such class where
there is in 'int' in another such class, but I'd try it anyway.


Cheers,

- Alf
 
M

maxime_phan

what i am surprised is that there is no escape door for this kind of
errors, after a few search on internet, i saw many people having this
kind of trouble especially using template (that can be seems normal if
different instanciation of template must have access to each other for
some reasons).
what i saw is to use pedwarn directly in the gcc source code to
transform this error in warning (if i understood well) it's a bit
strange..
 
M

maxime_phan max

I think i said something stupid about pedwarning, i just understand
what is it ;-)
 
A

Alf P. Steinbach

* maxime_phan max:
I think i said something stupid about pedwarning, i just understand
what is it ;-)

I don't know what it is but I think it's some internal thing in the gcc compiler.

Googling it the main reference I found was about your exact problem, the
friend-of-itself-class error, which has been reported before.

Sorry, I didn't think of keeping that URL.

But the upshot is I don't think there's an easy way to make g++ accept the code.

If the transform-friendship-into-base-class-access route seems impractical,
perhaps it's possible to use some other compiler? One idea might be to use the
Comeau front-end, if nothing else. You can check with the Comeau online tester
whether it accepts such code (I'm pretty sure that it does, but better check!).


Cheers,

- Alf
 
M

maxime_phan

hi, yes i will check that and think about other compilers..
thanks a lot for your help and advices!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top