2017-02-11 70 views
-1
#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的这需要在名称对象作为我的右手参数 //现在,当我添加了“”过载..它送给我的错误错误而运算符重载(错误:不对应的 '运营商<<'(操作数的类型是 '的std :: basic_ostream <char>' 和 '为const char [2]')

//错误是这样的: 错误:'操作符< <'(操作数类型是'std :: basic_ostream'和'const char [2]')不匹配 返回< < name.first < <“”< < name.second; ^

+0

错误消息中有很多有用的信息。 – juanchopanza

+0

你是什么意思? –

+0

我的意思是读取错误信息,看看你的代码,并找出它。或者如果您需要帮助,请发布[mcve]并解释您遇到的问题。 – juanchopanza

回答

1

感谢您上传所有细节 - 问题现在可以回答。

编译器无法达到您提供的重载,因为istream和istream在编译时不是完整的类型。

你需要写

#include <iostream> 

纠正这一点。有时候编译器诊断是迟钝的。

相关问题