繁体   English   中英

在std :: operator中不匹配“ operator <<”

[英]No match for “operator<<” in std::operator

#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()处收到错误,看来它无法打印返回void的内容。

std :: operator <<>( &std :: cout),(((const char

声明成员函数Bark一样

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

并称它为

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

或不更改功能,而是调用它

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

因为该函数的返回类型为void。

或者从狗窝里拿另一只狗。:)

树皮定义为void函数:

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

这意味着试图在main执行cout << buffy.Bark()尝试在cout中创建一个void类型变量,这是不可能的。 您可能只是说buffy.Bark(); ,这将已经为您输出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM