简体   繁体   English

使用字符串中的构造函数从 const char[] 转换为 class

[英]Conversion from const char[] to class with constructor from string

I try to pass a string to a function instead an object of class A , but I get an error.我尝试将字符串传递给 function 而不是 class A的 object ,但出现错误。 Why is it not working and how can I make it work?为什么它不工作,我怎样才能让它工作?

#include <string>

class A {
public:
    A(const std::string &str) {

    }
};

void test(const A &a) {

}

int main()
{
    A a("abc"); //it's ok
    test(a); //it's ok
    test("abc"); //error?
    return 0;
}

I don't want to pass a string object to test like test(std::string("abc")) .我不想传递一个字符串 object 来test test(std::string("abc")) I need convert it from const char[]我需要从 const char[] 转换它

In the implicit conversion sequence of one type to another, there is a rule that states: At most, only one user defined conversion can be applied.在一种类型到另一种类型的 隐式转换序列中,有一条规则规定:最多只能应用一个用户定义的转换。 In

test("abc");

You need two user defined conversions.您需要两个用户定义的转换。 First you need to conversion from const char[N] to std::string , which is considered a user defined conversion 1 .首先,您需要从const char[N]转换为std::string ,这被认为是用户定义的转换1 Then you need to convert that std::string to a A , which would be a second user defined conversion.然后您需要将该std::string转换为A ,这将是第二个用户定义的转换。 That is the reason you get the error这就是您收到错误的原因


To fix, you can use要修复,您可以使用

test(A("abc"));
//or
test(std::string("abc"));

and now you have one explicit user defined conversion, and one implicit conversion, satisfying the one implicit conversion rule.现在您有一个显式的用户定义转换和一个隐式转换,满足一个隐式转换规则。

You could also provide a constructor for A that accepts a const char* so that you can directly convert from "abc" to an A .您还可以为A提供一个接受const char*的构造函数,以便您可以直接从"abc"转换为A


1: Even though std::string is part of the standard library, it is not a "built in" type, but a library type. 1:尽管std::string是标准库的一部分,但它不是“内置”类型,而是库类型。 This means it is counted as a user defined type, even though it shipped with your implementation.这意味着它被视为用户定义的类型,即使它随您的实现一起提供。

The problem is that the standard only allow direct implicit conversions.问题是标准只允许直接隐式转换。

The following conversions are defined: const char* -> std::string and const std::string -> A定义了以下转换: const char* -> std::stringconst std::string -> A

That means that you can safely pass a const std::string where a A is expected, but not an implicitly convertible to std::string object.这意味着您可以安全地将const std::string传递到预期A的位置,但不能隐式转换为std::string object。 Transitivity does not work here.传递性在这里不起作用。

So you will have to do:所以你必须这样做:

test(std::string("abc"));    // ok...

The problem is with the conversions.问题在于转换。 "There can be only one" user defined conversion. “只能有一个”用户定义的转换。

Here is:这是:

 const char * -> std::string -> A

So you need to remove a conversion.所以你需要删除一个转换。 A way to fix it is to add the suffix 's' at the the end of the string:解决它的一种方法是在字符串末尾添加后缀“s”:

test("ABC"s);

https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s https://en.cppreference.com/w/cpp/string/basic_string/operator%22%22s

Another way is to add the function:另一种方法是添加function:

void test(const std::string& s)
{
    test(A(s)) ;
} 

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

相关问题 从const char *转换为const std :: string& - Conversion from const char * to const std::string & 在构造函数&#39;Player :: Player()&#39;中:错误:从&#39;const char *&#39;到&#39;char&#39;的无效转换 - In constructor ‘Player::Player()’: error: invalid conversion from ‘const char*’ to ‘char’ const unsigned char *到字符串或const char *的转换 - const unsigned char* conversion to/from string or const char* 从 initializer_list 转换<const char*>初始化器列表<string>在向量构造函数中</string></const> - Conversion from initializer_list<const char*> to initializer_list<string> in vector constructor 从“const char**”到“char* const*”的无效转换 - Invalid conversion from 'const char**' to 'char* const*' 从&#39;const char *&#39;到&#39;char&#39;的无效转换类错误 - invalid conversion from ‘const char*’ to ‘char’ Class Error 从“ const char *”到“ byte”的转换 - conversion from `const char*' to `byte' 没有从 DWORD 到 const char* 的转换 - No conversion from DWORD to const char* 如何修复“没有合适的转换函数从”String“到”const char *“存在”? - How to fix “no suitable conversion function from ”String“ to ”const char *“ exists”? 错误:从char到const char的无效转换* - error: invalid conversion from char to const char*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM