error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’ }

问题描述

报错:

 error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘void’)

std::shared_ptr<Cat> get_shared_ptr()
{
    std::shared_ptr<Cat> cat_p = std::make_shared<Cat>("local cat");
    return cat_p;
}
int main(int argc, char *argv[])
{

    cout<<"----3----"<<endl;
    std::shared_ptr<Cat> c_p = get_shared_ptr();
    c_p->cat_info();
    //cout<<c_p->cat_info()<<endl; //错误地方
    cout<<c_p->get_name()<<endl;

    cout<<"------yz------"<<endl;
    return 0;
}

cat.h

#ifndef CAT_H
#define CAT_H
#include <string>
#include <iostream>

class Cat
{
public:
    Cat(std::string name);
    Cat() = default;
    ~Cat();
    void cat_info() const
    {
        std::cout<<"cat info name : "<<name << std::endl;
    }
    std::string get_name() const
    {
        return name;
    }
    void set_cat_name(const std::string &name)
    {
        this->name=name;
    }
private:
    std::string name{"Mimi"};
};

#endif

cat.cpp

# include "/home/CLearn02/demo02/include/cat.h"

Cat::Cat(std::string name) : name(name) {
    std::cout<<"Constructor of Cat :"<< name <<std::endl;
}

Cat::~Cat()
{
    std::cout<<"Destructor of Cat:"<<name<<std::endl;
}

原因分析:

因为在类Cat中,cat_info()的返回值是void,"<<"不能接收void类型的返回值,所以cout<<c_p->cat_info()<<endl这条语句错误

直接用c_p->cat_info()输出即可;

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值