C++ 错误:'input >> Group1->Entrepreneur::Item'| 中的 'operator>>' 不匹配

2024-03-13

我收到此错误,并搜索了 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 object

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

这就是为什么你的

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

不编译。摆脱const在参数声明中。

一个不相关的问题

Points + 2;

这是无操作语句。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ 错误:'input >> Group1->Entrepreneur::Item'| 中的 'operator>>' 不匹配 的相关文章

  • 使用内部构造函数实例化类

    我有一个类 其构造函数被定义为内部 这意味着我无法实例化它 虽然这可能有道理 但出于调试和研究目的 我仍然愿意做一次 是否可以通过反射来做到这一点 我知道我可以访问私有 内部成员 但是我可以调用内部构造函数吗 或者 由于构造函数没有做任何重
  • 在两个 .cpp 文件之间定义全局变量 [重复]

    这个问题在这里已经有答案了 如何在 A cpp 和 B cpp 之间共享 全球化 bool 变量 其中它们都不包含其他 h 文件 他们有其他联合头文件 但彼此没有 我可以在这些共享标头中定义全局变量吗 Thanks 我可以在这些共享标头中定
  • C#9 顶级语句文件上的属性

    我正在尝试向顶级语句文件添加属性 但没有找到任何相关信息 是否可以 对于某些上下文 我想仅在该文件中禁用规则 SuppressMessage StyleCop CSharp LayoutRules SA1516 ElementsMustBe
  • 在 MVC 类上创建主键字段

    我是 MVC 和 C 新手 我只是偶然发现它并发现它很有趣 我遇到了一个不允许我继续的问题 这是我的代码 using System using System Collections Generic using System Linq usi
  • 为什么 LinkedList 通常比 List 慢?

    我开始在我的一些 C 算法中使用一些 LinkedList 而不是列表 希望能够加快速度 然而 我注意到他们只是感觉更慢 像任何优秀的开发人员一样 我认为我应该尽职调查并验证我的感受 所以我决定对一些简单的循环进行基准测试 我认为用一些随机
  • 将语句插入 SQL Server 数据库

    最近几天我试图找到这个错误 但没有成功 我正在尝试在数据库中插入一个新行 一切都很顺利 没有错误 也没有程序崩溃 My INSERT声明如下 INSERT INTO Polozaj Znesek Uporabnik Cas Kupec Po
  • 如何在 C++ 的子目录中创建文件?

    这是我的代码 如何在子目录联系人中创建文件 每次创建该文件时 它都会出现在与我的程序相同的目录中 int main ofstream myfile contacts myfile open a myfile close 在构造函数中指定完整
  • CMake 警告:无法为目标生成安全的链接器搜索路径

    在为 pcl 项目运行 CMake 时 我收到一条警告消息 Configuring done CMake Warning at CMakeLists txt 12 add executable Cannot generate a safe
  • Windows 程序如何临时更改其时区?

    我写了一个函数来返回time t与给定日期的午夜相对应的值 当给定日期没有午夜时 它返回最早可用的时间 例如 当埃及进入夏令时时 这种情况就可能发生 今年 时间更改于 4 月 29 日晚上午夜生效 因此时钟直接从 23 59 转到 01 0
  • 首先EntityFramework数据库 - 类型映射 - 将binary(8)从SQL映射到C#中的int

    在 SQL 内部 我有一个主键为二进制 8 的表 当我使用该表添加到我的模型中时Update Model from Database我可以看到该列有 type Binary 在 C 中 我将该列设为byte 我可以将该列映射到 int 吗
  • 使用正则表达式匹配以“Id”结尾的单词?

    如何组合一个正则表达式来匹配以 Id 结尾的单词并进行区分大小写的匹配 试试这个正则表达式 w Id b w 允许前面的单词字符Id和 b确保Id位于单词末尾 b是字边界断言
  • 使用 Microsoft Graph 创建用户

    如何使用 Microsoft graph 创建用户 因为我在保存过程中遇到了权限失败的问题 我确实有几个问题 在图中调用创建用户 API 将在哪里创建用户 是在 Azure AD 还是其他地方 我尝试通过传递 json 和必需的标头来调用创
  • 基于 C++ 范围的 for 循环

    尝试使用基于范围的 for 循环执行某些操作 可以使用常规的 for 循环来完成 如下所示 vector
  • ASP.NET Web API Swagger(Swashbuckle)重复OperationId

    I have a web api controller like below In swagger output I am having the below image And when I want to consume it in my
  • 为什么 GCC 6.3 在没有显式 C++11 支持的情况下编译此 Braced-Init-List 代码?

    我有一个问题大括号括起来的列表的不同含义 https stackoverflow com q 37682392 2642059 我知道C 03不支持C 11initializer list 然而 即使没有 std c 11编译器标志 gcc
  • 如何使用eclipse构建C++应用程序

    我已经从以下位置下载了 Eclipse Juno for C here http www eclipse org downloads download php file technology epp downloads release ju
  • 停止 TcpListener 的正确方法

    我目前正在使用 TcpListener 来处理传入连接 每个连接都有一个线程用于处理通信 然后关闭该单个连接 代码如下 TcpListener listener new TcpListener IPAddress Any Port Syst
  • C 中的静态和动态绑定(严格来说是 C,而不是 C++)是什么?

    我最初对发布这个问题感到担忧 以免它重复 但即使在谷歌搜索了许多关键字之后 我在 StackOverflow 上找不到任何解释 C 的静态和动态绑定的链接 尽管有 C 的问题和答案 但是都涉及classes以及显然不适合 C 的东西 Sta
  • Crypto++ 和压缩 EC 密钥

    如何在 Crypto 中生成压缩的 ECDSA 密钥 AutoSeededRandomPool prng ECDSA
  • 网页执行回发时如何停止在注册表单上?

    我正在做我的最后一年的项目 其中 我在一页上有登录和注册表单 WebForm 当用户点击锚点时Sign Up下拉菜单ddlType 隐藏 和文本框 txtCustName txtEmail and txtConfirmPassword 显示

随机推荐