简体   繁体   English

从具有用户定义的String类的返回类型的函数返回std :: string时出错

[英]Error when Returning std::string from a function with return type of user defined String class

I have defined my own string class in "String.cpp" (class String ), in some function with return type String , i am trying to return std::string , but it throws error Error: 我在“ String.cpp”(类String )中定义了自己的字符串类,在某些类型为返回类型String函数中,我试图返回std::string ,但它抛出错误Error:

Cannot use std::string  to initialize PiaStd::String

Please help 请帮忙

String System::getHostName()
{
    // in between code
    std::string result;
    struct addrinfo* p;
    for(p = ppResult; p != 0; p=p->ai_next)
    {
        result += std::string(p->ai_canonname);
    }
    Freeaddrinfo(ppResult);
    return result;
}

The problem is that the compiler does not know how to make your string from std::string . 问题是编译器不知道如何从std::string You need to provide a way to do it by writing one of the following: 您需要通过编写以下方法之一来提供一种方法:

  • Create a constructor of PiaStd::String that takes const std::string& as a parameter, or 创建一个PiaStd::String的构造函数,将const std::string&作为参数,或者
  • Create a custom conversion operator from std::string to PiaStd::String 创建一个从std::stringPiaStd::String的自定义转换运算符

You could also change getHostName() to make your custom string, or to return std::string . 您还可以更改getHostName()来创建自定义字符串,或者返回std::string

暂无
暂无

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

相关问题 如何从模板化类的方法返回std :: string,无论类型如何? - How to return std::string from method of templated class, no matter the type? 错误:使用 icc 17.0 时,不存在从“Data”到“std::__cxx11::string”的合适的用户定义转换 - error: no suitable user-defined conversion from “Data” to “std::__cxx11::string” exists when using icc 17.0 返回一个带有`std :: string`返回类型的本地char数组 - returning a local char array with `std::string` return type 在C ++中将用户定义的类型转换为std :: string - convert user-defined type to std::string in C++ 制作用户定义的类 std::to_string-able - Making a user-defined class std::to_string-able 是否可以使用cout从用户定义的类型自动转换为std :: string? - Is possible to get automatic cast from user-defined type to std::string using cout? 如何从(类)类型转换为std:string? - How to convert from type (Class) to std:string? 为什么在模板化类之外返回 std::string 的 constexpr 函数不会编译? - Why doesn't constexpr function returning std::string doesn't compile when outside templated class? 尝试返回std :: string时std :: logic_error - std::logic_error when trying to return a std::string 函数返回std :: string崩溃没有return语句,不像返回int而没有return语句的函数 - function returning std::string crashes without return statement, unlike a function returning int without return statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM