g++ 发出难以理解的警告
g++ issues incomprehensible warning
system.h:
#include <iostream>
namespace ss
{
class system
{
private:
// ...
public:
// ...
friend std::ostream& operator<< (std::ostream& out, const system& sys);
};
}
system.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
中声明。然而,它 是 在该名称空间内声明的。
我也试过用clang++
编译这个。 clang
只抱怨未使用的参数, 但不抱怨 我不明白的 'not within namespace' 问题。
g++
警告的原因是什么?这是误报吗?
版本:
g++ (GCC) 8.3.0
clang version: 8.00 (tags/RELEASE_800/final)
您只是错过了在 namespace
中声明 operator <<
。
尝试以下操作:
namespace ss
{
std::ostream& operator << (std::ostream& out, const system& sys);
class system
{
private:
// ...
public:
// ...
friend std::ostream& operator<< (std::ostream& out, const system& sys);
};
}
// in cpp
namespace ss
{
std::ostream& operator << (std::ostream& out, const system& sys)
{
// the body
}
}
system.h:
#include <iostream>
namespace ss
{
class system
{
private:
// ...
public:
// ...
friend std::ostream& operator<< (std::ostream& out, const system& sys);
};
}
system.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
中声明。然而,它 是 在该名称空间内声明的。
我也试过用clang++
编译这个。 clang
只抱怨未使用的参数, 但不抱怨 我不明白的 'not within namespace' 问题。
g++
警告的原因是什么?这是误报吗?
版本:
g++ (GCC) 8.3.0
clang version: 8.00 (tags/RELEASE_800/final)
您只是错过了在 namespace
中声明 operator <<
。
尝试以下操作:
namespace ss
{
std::ostream& operator << (std::ostream& out, const system& sys);
class system
{
private:
// ...
public:
// ...
friend std::ostream& operator<< (std::ostream& out, const system& sys);
};
}
// in cpp
namespace ss
{
std::ostream& operator << (std::ostream& out, const system& sys)
{
// the body
}
}