1

Consider the following code:

#include <stdio.h>

class A
{
    friend void foo(){ printf("%d\n",_a); }
public:
    A(int);
private:
    static const int _a=5;
};

class B
{
    friend void foo(){ printf("%d\n",_a); }
private:
    static const int _a=6;
};

int main()
{
    foo();
}

After compiling I've the following errors:

an_test.cpp:14:14: error: redefinition of ‘void foo()’
an_test.cpp:5:14: error: ‘void foo()’ previously defined here
an_test.cpp: In function ‘int main()’:
an_test.cpp:21:6: error: ‘foo’ was not declared in this scope
make: *** [an_test.o] Error 1

I think that functions defined with friend specifier are external linkage. So why an_test.cpp:21:6: error: ‘foo’ was not declared in this scope?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

3 Answers3

1

I think you are confusing linkage with scoping. In your code, the scope of both foo() definitions are inside their respective classes, so main() cannot see their definitions.

If you want foo() to be visible in main(), you need to move its declaration outside, and make a friend declaration inside the class.

#include <stdio.h>

class A
{
    friend void foo(A a);
    public:
    A(int) {}
    private:
    static const int _a=5;
};

void foo(A a){ printf("%d\n",a._a); }

int main()
{
    foo(A(5));
}

The following would also work, where the definition is inside and there is a declaration outside.

#include <stdio.h>

class A
{
friend void foo(A a){ printf("%d\n",a._a); }
public:
    A(int) {}
private:
    static const int _a=5;
};

 void foo(A a);


int main()
{
    foo(A(5));
}
merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • @Mankarse, I was just about to do the other test. – merlin2011 May 13 '14 at 04:34
  • But what about this quote "A function first declared in a friend declaration has external linkage (3.5)."?? In my case all function declared with friend declaration but they aren't visible inside the main(). –  May 13 '14 at 04:59
  • @DmitryFucintv, See [this question](http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c) for the difference between external linkage and internal linkage. It has nothing to do with scoping. – merlin2011 May 13 '14 at 05:03
1

I found something about C++ Friend functions that may help you know why the errors occur:

C++ Friend Functions

Note that:

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

nvg58
  • 891
  • 1
  • 8
  • 17
0

I think this link about friend scope can answer your question

Friend scope in C++

"Foo are friend of A" does not mean "Foo" are friend of "An_test"

Community
  • 1
  • 1
Hà Link
  • 367
  • 1
  • 6
  • 20