重载运算符<&lt ;.错误:二进制表达式的操作数无效

时间:2018-03-24 17:47:54

标签: c++ operator-overloading

我正在尝试重载一个<<运营商,但我有一个奇怪的问题。更确切地说是错误:

二进制表达式的操作数无效(' ostream'(又名' basic_ostream')和'团队')

团队是我的班级名称。

然而,我的朋友检查了我的代码,他说它正在为他工作。我在Mac上使用Xcode进行编码。

我认为它与编译器有关,不是吗?

我的代码分为main,.hpp文件和.cpp文件:

#include <stdio.h>
#include <iostream>  
#include "team.hpp"
int main()
{
    Team Legia = {"Legia Warszawa", "Warszawa", 283.5, 2, 42};
    Team Jagiellonia = {"Jagiellonia Bialystok", "Bialystok", 34.4, 1, 45};
    Legia.print();
    Jagiellonia.print();

    cout << Legia;
    cout << Jagiellonia;

    return 0;
}

.hpp文件

#include <stdio.h>
#include <iostream>
using namespace std;

class Team {
private:
    char *name;
    char *location;
    double budget;
    int place;
    int points;
public:
    Team();
    Team(const char*, const char*, double, int, int);

    friend ostream& operator<<(ostream& out, const Team &team);

};

.cpp文件

#include "team.hpp"
#include <iostream> 
#include <string.h>
using namespace std;


Team::Team(const char *_name, const char *_location, double _budget, int _place, int _points)
{
    name = new char[strlen(_name)+1];
    strcpy(name, _name);
    location = new char[strlen(_location)+1];
    strcpy(location, _location);
    budget = _budget;
    place = _place;
    points = _points;
}

Team::Team()
{
    name = new char[4];
    location = new char[8];
    strcpy(name, "name");
    strcpy(location, "location");
    budget = 0;
    place = 0;
    points = 0;
}

ostream& operator<<(ostream& out, const Team &team)
{
    out << "Informations about " << team.name << ": " << endl
    << "Location: " << team.location << endl
    << "Budget: " << team.budget << " millions (PLN)" << endl
    << "Place: " << team.place << endl
    << "Points: " << team.points << endl << endl;

    return out;
}

没有格式化的整个代码: http://coliru.stacked-crooked.com/a/7f0681a380324222

0 个答案:

没有答案