gpt4 book ai didi

c++ - g++ 发出难以理解的警告

转载 作者:可可西里 更新时间:2023-11-01 17:55:30 24 4
gpt4 key购买 nike

<分区>

系统.h:

#include <iostream>

namespace ss
{
class system
{
private:
// ...
public:
// ...
friend std::ostream& operator<< (std::ostream& out, const system& sys);

};
}

系统.cpp:

#include "system.h"

std::ostream& ss::operator<< (std::ostream& out, const ss::system& sys)
{
// print a representation of the ss::system
// ...
return out;
}

使用 g++ 8.30 编译以上代码会产生以下输出:

[db@dbPC test]$ LANG=en g++ -Wall -Wextra system.cpp
system.cpp:2:15: warning: 'std::ostream& ss::operator<<(std::ostream&, const ss::system&)' has not been declared within 'ss'
std::ostream& ss::operator<< (std::ostream& out, const ss::system& sys)
^~
In file included from system.cpp:1:
system.h:11:26: note: only here as a 'friend'
friend std::ostream& operator<< (std::ostream& out, const system& sys);
^~~~~~~~
system.cpp: In function 'std::ostream& ss::operator<<(std::ostream&, const ss::system&)':
system.cpp:2:68: warning: unused parameter 'sys' [-Wunused-parameter]
std::ostream& ss::operator<< (std::ostream& out, const ss::system& sys)
~~~~~~~~~~~~~~~~~~^~~

编译器告诉我,operator<<函数未在命名空间 ss 中声明.然而,它在该 namespace 内声明的。

我还尝试用 clang++ 编译它. clang只提示未使用的参数,但不我不明白的“不在 namespace 内”问题。

g++ 的原因是什么?警告?这是误报吗?

版本:

g++ (GCC) 8.3.0
clang version: 8.00 (tags/RELEASE_800/final)

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com