【问题标题】:Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}'错误:为“运算符 std::string {aka std::__cxx11::basic_string<char>}”指定的返回类型
【发布时间】:2020-03-27 14:05:58
【问题描述】:

错误的可能原因和解决方案是什么?

在.h文件中

std::string operator std::string();

在.c 文件中X 是一些int 变量 而Ychar

string ClassName::operator string()
{
    string temp;

    temp = X + Y;

    return temp;

}

【问题讨论】:

    标签: c++ string eclipse operator-overloading


    【解决方案1】:

    转换运算符的名称是它的返回类型。这意味着您没有为他们指定一个。那会给你

    operator std::string();
    

    对于声明和定义将是

    ClassName::operator string()
    {
        string temp;
    
        temp = X + Y;
    
        return temp;
    
    }
    

    还请注意,虽然temp = X + Y; 将编译,但它会为您提供一些X 放置在字符表中的字符。如果你想让123 + a 成为123a,那么你需要temp = std::to_string(X) + Y;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2021-10-28
      • 2021-09-16
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多