2015-06-23 149 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();,它已经为你输出了。

相关问题