运算符重载 C++;<< 操作的参数太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16291623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:13:50  来源:igfitidea点击:

Operator Overloading C++; too many parameters for << operation

c++operator-overloading

提问by Stanley Cup Phil

I have some code below that will take some names and ages and do some stuff with them. Eventually it will print them out. I need to change my print()function with a global operator<<. I saw on a different forumthat <<operatortakes two parameters, but when I try it I get a "too many parameters for << operation error. Is there something I am doing wrong? I am newer to C++ and I really do not get the point of operator overloading.

我在下面有一些代码,它们将采用一些名称和年龄并用它们做一些事情。最终它会将它们打印出来。我需要print()使用 global更改我的功能operator<<。我在一个不同的论坛上看到它<<operator需要两个参数,但是当我尝试它时,我得到一个“<<操作错误的参数太多。我做错了什么?我是 C++ 的新手,我真的不明白这一点运算符重载。

#include <iostream>;
#include <string>;
#include <vector>;
#include <string.h>;
#include <fstream>;
#include <algorithm>;

using namespace::std;

class Name_Pairs{
    vector<string> names;
    vector<double> ages;

public:
    void read_Names(/*string file*/){
        ifstream stream;
        string name;

        //Open new file
        stream.open("names.txt");
        //Read file
        while(getline(stream, name)){   
            //Push
            names.push_back(name);
        }
        //Close
        stream.close();
    }

    void read_Ages(){
        double age;
        //Prompt user for each age
        for(int x = 0; x < names.size(); x++)
        {
            cout << "How old is " + names[x] + "?  ";
            cin >> age;
            cout<<endl;
            //Push
            ages.push_back(age);
        }

    }

    bool sortNames(){
        int size = names.size();
        string tName;
        //Somethine went wrong
        if(size < 1) return false;
        //Temp
        vector<string> temp = names;
        vector<double> tempA = ages;
        //Sort Names
        sort(names.begin(), names.end());

        //High on performance, but ok for small amounts of data
        for (int x = 0; x < size; x++){
            tName = names[x];
            for (int y = 0; y < size; y++){
                //If the names are the same, then swap
                if (temp[y] == names[x]){
                    ages[x] = tempA[y];
                }
            }
        }
    }

    void print(){
        for(int x = 0; x < names.size(); x++){
            cout << names[x] << " " << ages[x] << endl;
        }
    }

    ostream& operator<<(ostream& out, int x){
        return out << names[x] << " " << ages[x] <<endl;
    }
};

回答by taocp

You are overloading <<operator as a member function, therefore, the first parameter is implicitly the calling object.

您将<<operator 作为成员函数重载,因此,第一个参数隐式是调用对象。

You should either overload it as friendfunction or as a free function. For example:

您应该将其作为friend函数或自由函数重载。例如:

overloading as friendfunction.

作为friend函数重载。

friend ostream& operator<<(ostream& out, int x){
     out << names[x] << " " << ages[x] <<endl;
     return out;
}

However, the canonical way is to overload it as freefunction. You can find very good information from this post: C++ operator overloading

但是,规范的方法是将其作为free函数重载。您可以从这篇文章中找到非常好的信息:C++ 运算符重载

回答by shivakumar

declare operator overloading function as friend.

将运算符重载函数声明为友元。

friend ostream& operator<<(ostream& out, int x)
{
        out << names[x] << " " << ages[x] <<endl;
        return out;
}