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

标签 c++

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

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

相关文章:

c++ - 即使包含库,g++ 也会提示 undefined reference

使用静态方法的 C++ 设计

使用 new 关键字创建的 C++ 对象不呈现;在堆栈上创建的对象确实

c++ - SFINAE 用于 CRTP 结构特征的特化

c++ - OpenGL,将纹理从图像应用到等值面

c++ - 在 C++ 中从字符串计算算术表达式

c++ - vector 和赋值运算符

c++ - cblas gemm 时间取决于输入矩阵值 - Ubuntu 14.04

c++ - 无法在 QWebEngineView 中登录 google : This app may not be secure

c++ - C++ 中的横向滚动条