在 befriended 类中声明的friend函数,GCC无法编译

发布于 2024-08-11 22:27:27 字数 441 浏览 10 评论 0原文

我有以下代码:

文件:Foo.h

class Foo {
    friend void Bar();
};

文件:Foo.cpp

void Bar() {};

文件 Test.cpp

#include "Foo.h"

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

VS2008 编译此代码,没有任何错误或警告。 G++ 4.3.4 报告:

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

为什么?

I've got following code:

File: Foo.h

class Foo {
    friend void Bar();
};

File: Foo.cpp

void Bar() {};

File Test.cpp

#include "Foo.h"

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

VS2008 compiles this without any error or warning. G++ 4.3.4 reports:

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

Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云之铃。 2024-08-18 22:27:27

我在“未回答”部分找到了这个问题,但是对之前错误答案的评论确实构成了正确答案。因此,这里是包含该内容的社区 wiki 响应。

摘要:GCC 似乎正在拒绝好的代码。

11.4.5 涵盖了在类内部定义(不仅仅是声明)友元函数的情况(“当且仅当该类是非局部类时,函数可以在类的友元声明中定义,函数名称是不合格的,并且该函数具有命名空间范围”——您的示例满足这些要求)。我想该标准确实允许在类内声明(“原型”)友元函数。这是 g++ 生成的错误让我烦恼。 – liori 11 月 22 日 20:35

另外 11.4.3:“在友元声明中首先声明的函数具有外部链接 (3.5)。否则,该函数将保留其先前的链接 (7.1.1)。”我认为这就能达成交易。 – Potatoswatter 0 秒前 [删除此评论]

I found this question in the "unanswered" section, but the comments to the previous incorrect answer do constitute a correct answer. Therefore here is a community wiki response with that content.

Summary: GCC appears to be rejecting good code.

case when a friend function is defined (not only declared) inside class is covered by 11.4.5 ("A function can be defined in a friend declaration of a class if and only if the class is a non-local class, the function name is unqualified, and the function has namespace scope" -- your example fulfils these requirements). I guess that standard does allow declaration ("prototype") of a friend function inside class. It is the error g++ is generating which bothers me. – liori Nov 22 at 20:35

Also 11.4.3: "A function first declared in a friend declaration has external linkage (3.5). Otherwise, the function retains its previous linkage (7.1.1)." I think that seals the deal. – Potatoswatter 0 secs ago [delete this comment]

等待圉鍢 2024-08-18 22:27:27

friend 声明不算作原型。您还需要一个单独的原型:

// File: Foo.h

void Bar();

class Foo {
    friend void Bar();
};

A friend declaration doesn't count as a prototype. You also need to need a separate prototype:

// File: Foo.h

void Bar();

class Foo {
    friend void Bar();
};
删除会话 2024-08-18 22:27:27

你们对标准的了解很摇滚,这是事实。但标准就是标准,和标准的实施是完全不同的事情。我猜你们一直只使用一个编译器,比如 MSVC,它似乎遵循标准。虽然还有大量其他编译器。

对于我和我 5 年的 C/C++ 经验来说,很明显 Test.cpp 没有看到 Bar,因为它只包含没有 Bar 声明的 Foo.h。

我给 John Kugelman +1,因为他的回答解决了所提出的问题。而且它不会利用标准知识发送垃圾邮件。

You guys rock with knowledge of the standard, that's true. But standard is standard, and implementation of the standard is completely different thing. I guess you guys are using just a single compiler all the time, like MSVC, that seems to follow the standard. While there are tons of other compilers out there.

For me and my humble 5 years experience with C/C++ this is obvious that the Test.cpp does not see the Bar, because it includes just Foo.h that does not have a declaration of Bar.

I gave John Kugelman +1 because his answer resolves the question asked. And it does not spam with knowlendge of standards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文