简体   繁体   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();
}

I'm newbie in C++ and I'm unable to figure out the error.I am getting the error at buffy.Bark(),it seems like its unable to print something which returns void. 我是C ++的新手,无法弄清错误。我在buffy.Bark()处收到错误,看来它无法打印返回void的内容。

no match for operator<< in std::operator<< >( &std::cout),((const char ) std :: operator <<>( &std :: cout),(((const char

Either declare member function Bark like 声明成员函数Bark一样

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

and call it like 并称它为

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

Or do not change the function but call it like 或不更改功能,而是调用它

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

because the function has return type void. 因为该函数的返回类型为void。

Or take another dog from the dog kennel.:) 或者从狗窝里拿另一只狗。:)

Bark is defined as a void function: 树皮定义为void函数:

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

This means that trying to do cout << buffy.Bark() in main is trying to cout a void type variable, which is not possible. 这意味着试图在main执行cout << buffy.Bark()尝试在cout中创建一个void类型变量,这是不可能的。 It's likely you simply meant buffy.Bark(); 您可能只是说buffy.Bark(); , which will already output for you. ,这将已经为您输出。

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

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