作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
因此,我有一个问题是我最近尝试重载<<运算符时遇到的错误。
我有一个名为“structPixels.h”的文件,在其中我定义了一个结构,如下所示:
#pragma once
#include <iostream>
namespace Eng
{
/**
* A structure to represent pixels
*/
typedef struct Pixel{
unsigned int x; ///The x-coordinate
unsigned int y; ///The y-coordinate
bool operator ==(const Pixel& rhs) const
{
return (this->x == rhs.x && this->y == rhs.y);
};
bool operator !=(const Pixel& rhs) const
{
return (this->x != rhs.x || this->y != rhs.y);
};
bool operator <(const Pixel& rhs) const
{
return std::tie(this->x, this->y) < std::tie(rhs.x, rhs.y);
}
friend std::ostream& operator << (std::ostream& out, const Pixel& p);
} Pixel;
}//End of namespace Eng
namespace std {
//Removing the inline does not fix the error. Rather, it fixed another error
//which I had with duplicate symbols
inline ostream& operator<<(ostream &os, const Eng::Pixel &p)
{
os << "{"
<< p.x
<< ","
<< p.y
<< "}";
return os;
}
}//End of namespace std
#include "structPixels.h"
Pixel test = {3, 4};
std::cout << "Test: " << test << std::endl;
error: use of overloaded operator '<<' is ambiguous (with operand types 'basic_ostream<char, std::__1::char_traits<char> >' and 'Eng::Pixel')
std::cout << "Test: " << test << std::endl;
~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~
namespace Eng
{
/**
* A structure to represent pixels
*/
typedef struct Pixel{
unsigned int x; ///The x-coordinate
unsigned int y; ///The y-coordinate
bool operator ==(const Pixel& rhs) const
{
return (this->x == rhs.x && this->y == rhs.y);
};
bool operator !=(const Pixel& rhs) const
{
return (this->x != rhs.x || this->y != rhs.y);
};
bool operator <(const Pixel& rhs) const
{
return std::tie(this->x, this->y) < std::tie(rhs.x, rhs.y);
}
} Pixel;
inline std::ostream& operator<<(std::ostream& os, const Pixel& rhs)
{
os << "{" << rhs.x << "," << rhs.y << "}";
return os;
}
}//End of namespace Eng
最佳答案
首先,您不能将函数重载添加到std
中。这样做是未定义的行为。你想做的就是放
inline std::ostream& operator<<(std::ostream &os, const Eng::Pixel &p)
{
os << "{"
<< p.x
<< ","
<< p.y
<< "}";
return os;
}
Eng
命名空间中。这样做
allows the code to compile just fine。在 namespace 中使用运算符还可以使
ADL工作,因此即使用户的代码中没有
using namespace Eng;
也会找到运算符。
friend std::ostream& operator << (std::ostream& out, const Pixel& p);
std
中定义的函数。由于这些函数在不同的命名空间中,因此它们被视为重载,并且由于两者相等,因此会出现歧义错误。
关于c++ - 重载<<运算符导致其模棱两可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963662/
我从一所大学获得了一些示例代码,导入了项目并尝试运行测试:方法 assertThat(Integer, Matcher) 对于 MyClass 类型是不明确的 每个 assertThat 都被标记为红
关于将 iOS 应用程序迁移到 Swift 3.0 的过程。这是我面临的一个问题。 先上相关代码: let calendar = NSCalendar.current, calendCompo = c
我刚开始研究 Java 8 Lambda 功能。我在 Java 7 中编写了这段代码,并尝试在 lamdas 中执行它。请注意,最后一行会产生编译错误,因为重载的函数不明确。我明白原因。如何使用 la
如何优先(告诉编译器)使用“函数接收引用”(#B)而不是“函数接收值”(#A)? #include using namespace std; class Heavy{/* ...... */}; /
我正在使用 Google Map API V3 显示车辆行驶路径及其路线方向。但是通过谷歌方向图标,很难找到方向。下图解释更多 我看到了每个图标,它是 source 我找到了图片路径,是 http:/
我是一名优秀的程序员,十分优秀!