[英]error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’
[英]Error while operator overloading (error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const char [2]’)
#ifndef NAME_H
#define NAME_H
#include <string> // For string class
class Name
{
private:
std::string first{};
std::string second{};
public:
Name(const std::string& name1, const std::string& name2) : first(name1), second(name2){}
Name()=default;
std::string get_first() const {return first;}
std::string get_second() const { return second; }
friend std::istream& operator>>(std::istream& in, Name& name);
friend std::ostream& operator<<(std::ostream& out, const Name& name);
};
// Stream input for Name objects
inline std::istream& operator>>(std::istream& in, Name& name)
{
return in >> name.first >> name.second;
}
// Stream output for Name objects
inline std::ostream& operator<<(std::ostream& out, const Name& name)
{
//My error is here while I am adding " " in the overload
return out << name.first << " " << name.second;
}
我想重载std::ostream
的<<
运算符,它将 Name 对象作为我的右手参数。
现在,当我在重载中添加“”时..它向我显示了错误
错误是这样的:
error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘const char [2]’)
return out << name.first << " " << name.second;
^
感谢您上传所有详细信息 - 现在可以回答问题了。
编译器无法访问您提供的重载,因为istream
和ostream
在编译时不是完整类型。
你需要写
#include <iostream>
纠正这一点。 有时编译器诊断是迟钝的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.