C++ 错误 : no match for 'operator>>' in 'input >> Group1->Entrepreneur::Item' |

标签 c++

我遇到了这个错误,并且已经搜索了 4-6 个小时来尝试找到解决方案,但是在我的特定情况下我的搜索都没有产生任何结果,所以这是我的 main.cpp。

#include <iostream>
#include <string>
#include "Entrepreneur.h"
#include <fstream>

using namespace std;

bool operator >(const Entrepreneur & Group1,const Entrepreneur & Group2)
{
    if((Group1.Points > Group2.Points || Group1.Points == Group2.Points) && Group1.Profit > Group2.Profit )
    {
        return true;
    }
    else
    {
        return false;
    };
};

ostream &operator<<( ostream &output,const Entrepreneur &Group1)
{
 output <<"\nItem : " << Group1.Item << "\nNr : " << Group1.Nr << "\nDonation : R" << Group1.Donation << "\nStart up amount : R" << Group1.StartUpAmt << "\nExpenses : R" << Group1.Expenses <<"\nPoints : " << Group1.Points << "\nSold : " << Group1.Sold << endl;;
 return output;
};

    istream &operator>>( istream  &input,const Entrepreneur & Group1)
{
    cout << "Enter Items to be sold,Donation amount,number of members & startup amount. Seperated by a space." << endl;

    input >> Group1.Item >> Group1.Donation >> Group1.Nr >> Group1.StartUpAmt;
    return input;
};

int main()
{


    return 0;
};

这是我的头文件。

#ifndef ENTREPRENEUR_H_INCLUDED
#define ENTREPRENEUR_H_INCLUDED
#include <iostream>
#include <string>

using namespace std;

class Entrepreneur{
    private:
        string Item;
        int Nr;
        double Donation;
        double StartUpAmt;
        double Expenses;
        double Income;
        double Profit;
        int Points;
        bool Sold;
    public:
        Entrepreneur(){
            Item = "Not Decided";
            Nr = 1;
            Donation = 0;
            StartUpAmt = 0;
            Expenses = 0;
            Income = 0;
            Profit = 0;
            Points = 0;
            Sold = 0;
        };
        void CreatGroup(string iItem,int iNr,double iDonation,double iStartUpAmt){
            Item = iItem;
            Nr = iNr;
            Donation = iDonation;
            StartUpAmt = iStartUpAmt;
        };
        void DisplayGroup(){
            cout << "\nItem : " << Item << "\nNr : " << Nr << "\nDonation : R" << Donation << "\nStart up amount : R" << StartUpAmt << "\nExpenses : R" << Expenses << "\nIncome : R" << Income << "\nProfit : R" << Profit << "\nPoints : " << Points << "\nSold : " << Sold << endl;
        };
        void set_info(double iExpenses,double iIncome,bool iSold){
            Expenses = iExpenses;
            Income = iIncome;
            Sold = iSold;
        };
        void calc_profit(double iDonation,double iStartUpAmt,double iExpenses,double iIncome){
            Donation = iDonation;
            StartUpAmt = iStartUpAmt;
            Expenses = iExpenses;
            Income = iIncome;

            Profit = Income + (StartUpAmt + Donation) - Expenses;
        };
        void update_points(){
            Points = 0;

            if(Nr < 3)
            {
                Points ++;
            }
            else
            {
                Points + 2;
            }
            if(Donation == 0)
            {
                Points ++;
            }
            if(Sold == 1)
            {
                Points ++;
            }
        };
        void Display(){
            cout << "Congratulations to all groups that partook in the challenge !" << endl;
        };

        friend bool operator >(const Entrepreneur & Group1,const Entrepreneur & Group2);
        friend ostream &operator<<( ostream &output,const Entrepreneur & Group1);
        friend istream &operator>>( istream  &input,const Entrepreneur & Group1);


};

#endif // ENTREPRENEUR_H_INCLUDED

所以错误来自重载运算符的友元成员>> 在 Entrepreneur.h 的企业家类中

friend istream &operator>>( istream  &input,const Entrepreneur & Group1);

在 main.cpp 中:

istream &operator>>( istream  &input,const Entrepreneur & Group1)
{
    cout << "Enter Items to be sold,Donation amount,number of members & startup amount. Seperated by a space." << endl;

    input >> Group1.Item >> Group1.Donation >> Group1.Nr >> Group1.StartUpAmt;
    return input;
};

我在谷歌上通常 10 分钟就在这里碰壁了,我会修复它,但是这个让我变得更好。 PS当我开始测试时,这个程序还没有完成。 提前致谢。

最佳答案

您正在尝试读入一个const 对象

istream &operator>>(istream  &input, const Entrepreneur & Group1)

这就是为什么你的

input >> Group1.Item >> Group1.Donation >> Group1.Nr >> Group1.StartUpAmt;

不编译。去掉参数声明中的 const

一个不相关的问题

Points + 2;

这是空操作声明。

关于C++ 错误 : no match for 'operator>>' in 'input >> Group1->Entrepreneur::Item' |,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36407380/

相关文章:

c++ - 在 CLion 中调试时程序收到段错误

c++ - boolean 数组的求和速度

c++ - 在c++中,我们能否通过volatile +内存栅栏(sfence + lfence)保证两个线程之间发生事前?

c++ - 透明配置 std::function/lambda 回调

c++ - 将适当的 PHI 节点添加到 llvm-ir

java - 无法启动应用程序调试 - 在 Eclipse 中组合 C/java android 应用程序

c++ - g++ 中的模板规范编译错误

c++ - 是否可以在不知道名称的情况下访问函数参数

All or Nothing 工作的 C++ 类似事务的模式

c++ - 如何让 QML map 流量通过代理,并为程序的其余部分禁用该代理?