Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: friend functions and extern "C"

392 views
Skip to first unread message

Gil Sudai

unread,
Aug 8, 1999, 3:00:00 AM8/8/99
to
I have a problem declaring a friend function that is declared as extern "C".
This is the situation:

/*foo.h - Foo declaration*/
extern "C"
int Foo (int x, int y);

/*foo.cpp - Foo implementation*/
extern "C"
int Foo (int x, int y) {
...
}

/*boo.h - declare the class*/
class CBoo
{
...
friend int Foo (int x, int y); // Foo is extern "C"
};

What i get is a compiler error (on VC6)
C2732: linkage specification contradicts earlier specification for Foo
in foo.cpp where Foo is implemented.
What is the problem and how do I fix it???

Gil Sudai.

Andreas Huber

unread,
Aug 8, 1999, 3:00:00 AM8/8/99
to
Gil,

the following compiles clean in VC++ 5.0:

*** foo.h ***

extern "C"
{
int foo(int x, int y);
}


class CBoo {

friend int foo(int x, int y);

private:
int m_n;

};

*** foo.cpp

#include "foo.h"

extern "C"
{
int foo(int x, int y)
{
CBoo * pBoo = new CBoo();
return x*y*pBoo->m_n;
}
}

void main()
{
foo(1,1);
}

Don't know what the problem is with your code, apart from the fact that
extern "C" likes {} around it's "body".

HTH Andreas


Gil Sudai <a...@abc.com> schrieb in im Newsbeitrag:
7ojsa7$487$1...@news.netvision.net.il...

George Privalov

unread,
Aug 9, 1999, 3:00:00 AM8/9/99
to
Beg you pardon, but I don't understand what's the point of declaring C
function as friend? You still can't access the class members from there.
If you declare some structure in C that will be equivalent to C++ class and
pass ref on it as a parameter then you don't care about private members.
Anyway you are defying the principles that C++ was designed on so good luck.

George

Gil Sudai wrote in message <7ojsa7$487$1...@news.netvision.net.il>...

Gil Sudai

unread,
Aug 9, 1999, 3:00:00 AM8/9/99
to
the extern "C" function is a c++ function that one of it's arguments is a
ptr to my class.
it is declared as extern "C" only to allow c functions call it. in c, my
class ptr is typedefed to void* and only treated in my c++ functions (all of
them are extern "C").
this is unusual situation, i know, but that's life...

George Privalov <george_...@email.msn.com> wrote in message
news:enYAeUi4#GA.154@cpmsnbbsa02...
>

0 new messages