C++ 操作员过载时发出警告<&书信电报;对于命名空间中的类
当重载C++ 操作员过载时发出警告<&书信电报;对于命名空间中的类,c++,namespaces,overloading,C++,Namespaces,Overloading,当重载时,我会收到一条警告(尽管有效),只需删除形状::: std::ostream& operator<<(std::ostream &os, const Rectangle &rectangle) { os << "rectangle"; return os; } 我找到了 您必须在名称空间中而不是在类中定义重载方法 #include <ostream> namespace Shape {
时,我会收到一条警告(尽管有效),只需删除形状::
:
std::ostream& operator<<(std::ostream &os, const Rectangle &rectangle) {
os << "rectangle";
return os;
}
我找到了
您必须在名称空间中而不是在类中定义重载方法
#include <ostream>
namespace Shape {
class Rectangle {
};
std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
}
#包括
名称空间形状{
类矩形{
};
std::ostream&operator非常感谢你,我猜不出这个!!如果你这样做,那么这个操作符是有效的,但是如果我调用矩形函数,它会产生一个错误,就像这样cout@Brian是的,但是我怎么能不得到警告呢?在函数周围添加名称空间形状{
..}
。
warning: 'std::ostream& Shape::operator<<(std::ostream&, const Shape::Rectangle&)' has not been declared within Shape
std::ostream& Shape::operator<<(std::ostream &os, const Rectangle &rectangle) {
^~~~~
note: only here as a friend
friend std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
^~~~~~~~
std::ostream& operator<<(std::ostream &os, const Rectangle &rectangle) {
os << "rectangle";
return os;
}
namespace Shape {
class Rectangle {
public:
friend std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
};
std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
}
#include <ostream>
namespace Shape {
class Rectangle {
};
std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
}
#include <ostream>
namespace Shape {
class Rectangle {
public:
friend std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
};
}