[英]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: 您需要通过编写以下方法之一来提供一种方法:
PiaStd::String
that takes const std::string&
as a parameter, or PiaStd::String
的构造函数,将const std::string&
作为参数,或者 std::string
to PiaStd::String
std::string
到PiaStd::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.