2016-04-06 32 views
0

Entrepreneur sınıfım için hem akış yerleştirme hem de çıkarma işlecini aşırı yüklemeye çalışıyorum. Benim main.cpp dosyasında Aşırı yükleme akışı ekleme ve çıkarma işleci

friend istream& Entrepreneur::operator>> (istream &input, Entrepreneur &entrepreneur) { 
    cout << "Please enter the item to be sold: "; 
    input >> entrepreneur.Item; 
    cout << "\nPlease enter the donation amount received: "; 
    input >> entrepreneur.Donation; 
    cout << "\nPlease enter the amount of members in the group: "; 
    input >> entrepreneur.Nr; 
    cout << "\nPlease enter the startup amount received: "; 
    input >> entrepreneur.StartupAmt; 
    cout << endl; 
    return input; 
} 

friend ostream& Entrepreneur::operator<< (ostream &output, Entrepreneur &entrepreneur) { 
    output << "Item: " << entrepreneur.Item << endl; 
    output << "Members in group: " << entrepreneur.Nr << endl; 
    output << "Startup amount: " << entrepreneur.StartupAmt << endl; 
    output << "Donation amount: " << entrepreneur.Donation << endl; 
    output << "Expenses: " << entrepreneur.Expenses << endl; 
    output << "Points earned: " << entrepreneur.Points << endl; 
    output << "All items sold: " << entrepreneur.Sold ? "Yes" : "No" << endl; 
    return output; 
} 

, ben aşağıdaki kodu çalışıyorum:

benim Girişimci sınıfında aşağıdaki var

int main() { 
    Entrepreneur Group3; 
    cin >> Group3; 
} 

kod derlenemiyor. Şu hata mesajını alıyorum:

siz yardım edebilir

İkili Operatörü '>>' türü 'istream' ve 'Girişimci' ifadesi için uygulanamaz Yukarıdaki kodda neyin yanlış olduğunu anlamaya mıyım?

+0

Yardımın için teşekkürler. Bunu cevap olarak gönderebilir misiniz, böylece kabul edebilirim! –

+1

Bu size bazı ek açıklamalar vermelidir: http://stackoverflow.com/questions/236801/should-operator-be-implemented-as-a-friend-or-as-a-member-function – Christophe

cevap

1

İmzalar yanlış. Üye değilsiniz, çünkü üye olmayan işlevleri bildirmek/tanımlamak istersiniz. Enterpreneur::'u bırakın ve sorun çözüldü.

sınıf tanımı gibi görünmelidir:

class Enterpreneur 
{ 
public: 
    ... 

    friend istream& operator>> (istream &input, Entrepreneur &entrepreneur); 
    friend ostream& operator<< (ostream &output, Entrepreneur const& entrepreneur); 
    //              ^^^^^ 
    //         we're not modifying the argument, are we? 
}; 

ve sonra tıpkı diğer üye olmayan fonksiyonu olarak bu operatörler (hayır friend anahtar kelime) tanımlamak veya sınıfla satır içi tanımlayın.

+0

Yardımlarınız için teşekkürler. Şimdi derler ama şimdi bu hata mesajını alıyorum: ‘arkadaş’ sınıfın dışında kullanıldı –

+0

@Henry Düzenlemeye bakın. – LogicStuff

+1

Teşekkürler, şimdi çalışıyor! –