2016-04-04 2 views
0

Я получаю эту ошибку и искал хороший 4-6 часов, чтобы попытаться найти решение, но ни один из моих поисков не дал никаких результатов в моей конкретной ситуации, поэтому вот мой main.cpp.Ошибка C++: нет соответствия для 'operator >>' in 'input >> Group1-> Entrepreneur :: Item' |

#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 минут на google, и я бы исправил ее, но этот стал лучше меня. PS программа не закончена, обнаружил это, когда я начал тестировать. Спасибо заранее.

ответ

2

Вы пытаетесь прочитать в const объекта

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

Вот почему ваш

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

не компилируется. Избавиться от const в объявлении параметра.

неродственного вопрос

Points + 2; 

Это не оп заявление.

+0

Спасибо Я попытался удалить константу, но затем я получаю сообщение об ошибке, потому что переменная Item в классе Entrepreneur является частной, даже если ее функция друга пытается получить к ней доступ. И спасибо на второй момент – Divinator

+0

Спасибо, что это сработало, я просто забыл удалить cont из функции friend в файле заголовка снова, спасибо большое – Divinator