2015-06-23 155 views
1
#include <iostream> 
#include <string> 
using namespace std; 

// your code 
class Dog { 
public: 
    int age; 
    string name, race, voice; 

    Dog(int new_age,string new_name,string new_race,string new_voice); 
    void PrintInformation(); 
    void Bark(); 
}; 

    Dog::Dog(int new_age,string new_name,string new_race,string new_voice) { 
     age = new_age; 
     name = new_name; 
     race = new_race; 
     voice = new_voice; 
    } 

    void Dog::PrintInformation() { 
     cout << "Name: " << name; 
     cout << "\nAge: " << age; 
     cout << "\nRace: " << race << endl; 
    } 

    void Dog::Bark(){ 
     cout << voice << endl; 
    } 


int main() 
{ 
    Dog buffy(2, "Buffy", "Bulldog", "Hau!!!"); 
    buffy.PrintInformation(); 
    cout << "Dog says: " << buffy.Bark(); 
} 

我是C++中的新手,我無法弄清錯誤。我在buffy.Bark()看到錯誤,它似乎像它無法打印返回無效的東西。std :: operator中的「operator <<」不匹配

在標準::操作者< <>(&的std :: COUT),((常量字符

+0

類型的表達式的值的'buffy.Bark()'是'功能::狗的Bark'返回類型。這種類型看起來是可打印的嗎? –

+0

@Kerrek Sb不,它不 –

回答

2

要麼聲明成員函數Bark

std::string Dog::Bark(){ 
    return voice; 
} 

,並調用它像

cout << "Dog says: " << buffy.Bark() << endl; 

還是不改變功能,但這樣稱呼它

cout << "Dog says: "; 
buffy.Bark(); 

,因爲函數返回鍵入void。

或採取從狗窩另一隻狗。:)

0

樹皮被定義爲空隙功能無法與操作者< <:

void Dog::Bark(){ 
    cout << voice << endl; 
} 

這意味着試圖做cout << buffy.Bark()main正試圖做一個void類型變量,這是不可能的。很可能你的意思是buffy.Bark();,它已經爲你輸出了。

相關問題