gpt4 book ai didi

c++ - 重载<<运算符导致其模棱两可

转载 作者:行者123 更新时间:2023-12-01 15:05:02 25 4
gpt4 key购买 nike

因此,我有一个问题是我最近尝试重载<<运算符时遇到的错误。

我有一个名为“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

但是,当我创建它并按如下所示调用cout时:

#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);

通过ADL,它将找到您在 std中定义的函数。由于这些函数在不同的命名空间中,因此它们被视为重载,并且由于两者相等,因此会出现歧义错误。

关于c++ - 重载<<运算符导致其模棱两可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963662/

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