Friend function declared inside a subclass of a class, GCC does not compile

I have the following code:

File: Foo.h

class Foo {
    friend void Bar();
};

File: Foo.cpp

void Bar() {};

Test.cpp File

#include "Foo.h"

int main(void) {
    Bar();
    return 0;
}

VS2008 compiles this without any errors or warnings. G ++ 4.3.4 reports:

test.cpp: In function ‘int main()’:
test.cpp:8: error: ‘Bar’ was not declared in this scope

Why?

+3
source share
3 answers

I found this question in the "unanswered" section, but comments on the previous incorrect answer make up the correct answer. Therefore, the community wiki response with this content is reported here.

Summary: GCC seems to reject good code.

, ( ) , 11.4.5 ( " , , , " - ). , ("prototype") friend . g++, . - liori 22 20:35

11.4.3: ", , (3.5). (7.1.1)". , . - Potatoswatter 0 [ ]

+2

friend . :

// File: Foo.h

void Bar();

class Foo {
    friend void Bar();
};
+1

, , , . , - . , , , , MSVC, , , . .

5- C/++ , Test.cpp Bar, Foo.h, Bar.

+1, . knowlendge .

-2

Source: https://habr.com/ru/post/1723613/


All Articles