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;
}
Compiling the above code with g++ 8.30 yields the following output:
[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)
~~~~~~~~~~~~~~~~~~^~~
The compiler tells me, that the operator<<
function was not declared within the namespace ss
. However it is declared within that namespace.
I also tried to compile this with clang++
. clang
complains only about the unused parameter, but not about the 'not within namespace' issue I do not understand.
What is the cause of the g++
warning? Is this a false warning?
Versions:
g++ (GCC) 8.3.0
clang version: 8.00 (tags/RELEASE_800/final)
You simply missed declaring the operator <<
in namespace
.
Try the following:
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With