c++ - Visual Studio 编译并检测操作符重载,而 g++ 则不然

标签 c++ c++11 visual-studio-2013 g++

我有一个家庭类。我使用 house house; 初始化一个新的 house 元素,然后将数据传递到其中,然后计算它:

cout << house;

Couting house 在 Visual Studio 中工作得很好,但由于某种原因,当我尝试使用 g++ 编译时收到此错误:

main.cpp:19:57: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>::__ostream_type {aka std::basic_ostream<char>}’ and ‘house’)
cout << "\nnext house to be visited:" << endl << endl << house << endl;

尽管我非常清楚地在我的头文件之一中包含此内容:

friend std::ostream& operator<< (std::ostream& out, house);

您能提供的任何反馈都将不胜感激,因为我认为 g++ 没有理由无法看到我的运算符重载函数。

编辑:这是我的运算符重载函数:

std::ostream& operator<< (std::ostream& out, const house& house)
{
    out << "Address: " << house.getAddress() << std::endl
        << "Square Feet: " << house.getSqrFt() << std::endl
        << "Bedrooms: " << +house.getBedrooms() << std::endl
        << "Bathrooms: " << house.getBathrooms() << std::endl
        << "Description: " << house.getDescription() << std::endl;
    return out;
}

这是我的家庭类(class):

#ifndef HOUSE
#define HOUSE
class house
{
public:
    house();
    house(const char[], const unsigned short& sqrFt, const unsigned char& bedrooms, const float& bathrooms, const char[]);
    house(house & obj);
    house(house *& obj);
    ~house();
    char * getAddress() const;
    unsigned short getSqrFt() const;
    unsigned char getBedrooms() const;
    float getBathrooms() const;
    char * getDescription() const;
    void setAddress(const char address[]);
    void setSqrFt(const unsigned short& sqrFt);
    void setBedrooms(const unsigned char& bedrooms);
    void setBathrooms(const float& bathrooms);
    void setDescription(const char description[]);
    void setEqual(house &, house*);
private:
    char * address;
    unsigned short sqrFt;
    unsigned char bedrooms;
    float bathrooms;
    char * description;
};
#endif

这是我的队列类,其中包含运算符重载函数的声明:

#ifndef QUEUE
#define QUEUE
#include <ostream>
#include "house.h"

class queue
{
public:
    queue();
    queue(queue & obj);
    ~queue();
    void enqueue(house *& item);
    bool dequeue(house & item);
    void print() const;
    void readIn(const char []);
private:
    struct node
    {
        node();
        house* item;
        node * next;
    }; 
    node * head;
    node * tail;
    void getLine(std::ifstream&, char key[]);
    friend std::ostream& operator<< (std::ostream& out, const char[]);
    //friend std::ostream& operator<< (std::ostream& out, house *&);
    friend std::ostream& operator<< (std::ostream& out, const house&);
};
#endif

最佳答案

问题是您正在声明您的 operator<<对于 house在错误的类(class)中:

class queue
{
    friend std::ostream& operator<< (std::ostream& out, const house&);
};

当你在类中声明友元运算符时X ,只有当我们查找 X 时,该运算符的查找才会成功。 。通过这个声明,我们只会找到 operator<<(std::ostream&, const house&)当我们查找queue时- 但这是不可能的,因为没有一个参数是 queue所以我们永远不会尝试用一个来查找它。

您需要将声明移至正确的类:

class house {
    friend std::ostream& operator<< (std::ostream& out, const house&);
};

关于c++ - Visual Studio 编译并检测操作符重载,而 g++ 则不然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375770/

相关文章:

c++ - DirectX 9 固定功能纹理

c++ - 使用 getline 读取文件和标准输入的结果不同

c++ - 原始指针和智能指针混合函数重载

c++ - 将实验性命名空间注入(inject) std

c++ - 将通用引用转换为可调用的 void 指针,反之亦然

visual-studio-2013 - 在 Visual Studio 2013 中包含 Expat 库

visual-studio-2013 - VS2013无法从“工具”>“导入和导出设置”导入设置

c++ - 从 'char' 到 'const char*' 的无效转换

c++ - 如何在 C++ 中通过引用传递堆栈矩阵

visual-studio-2013 - 即使通过 NuGet 安装,SpecFlow 也找不到包