简体   繁体   English

C ++错误:无法转换&#39;std :: basic_string <char> &#39;至&#39;const char *&#39;

[英]C++ error: cannot convert ‘std::basic_string<char>’ to ‘const char*’

I'm using a function to download a file. 我正在使用一个功能来下载文件。

void downloadFile(const char* url, const char* fname) {
  //..
}

This is called like : 这叫做:

downloadFile("http://servera.com/file.txt", "/user/tmp/file.txt");

This working fine. 这工作正常。

But I want to change the URL to be a value from an array. 但我想将URL更改为数组中的值。 The array stores encrypted values which when decrypted are strings, so I get the issue error: cannot convert 'std::basic_string<char>' to 'const char*' 该数组存储加密的值,该值在解密时为字符串,因此出现问题error: cannot convert 'std::basic_string<char>' to 'const char*'

I've tried: 我试过了:

 string test = decode(foo[5]);
 const char* t1= test.c_str();
 downloadFile(t1 "filename.txt", "/user/tmp/file.txt");

downloadFile(t1 + "filename.txt", "/user/tmp/file.txt");

and

downloadFile((decode(foo[5]).c_str()) + "filename.txt", "/user/tmp/file.txt");

which gives: error: invalid operands of types 'const char*' and 'const char [17]' to binary 'operator+' 给出: error: invalid operands of types 'const char*' and 'const char [17]' to binary 'operator+'

What am I doing wrong ? 我究竟做错了什么 ?

Thanks 谢谢

C-strings can't be concatenated with + . C字符串不能与+串联。

Use std::string::+ instead: 使用std::string::+代替:

downloadFile((test + "filename.txt").c_str(), "/user/tmp/file.txt");

Note that c_str only returns a pointer to the std::string 's internal character array, so it's valid only during the execution of the downloadFile function. 请注意, c_str仅返回指向std::string内部字符数组的指针,因此它仅在执行downloadFile函数期间有效。

Try this: 尝试这个:

downloadFile((decode(foo[5]) + "filename.txt").c_str(), "/user/tmp/file.txt");

The operator+ is not defined for char arrays. 没有为char数组定义operator +。

The main problem in your code is that you are trying to use operator+ to concatenate raw C strings (ie raw const char* pointers, or raw char [] arrays), which doesn't work. 代码中的主要问题是,您试图使用operator+连接原始C字符串 (即原始const char*指针或原始char []数组),但无法正常工作。

In C, you should use proper library functions (like strncat or safer variants) to do that; 在C语言中,您应该使用适当的库函数(例如strncat或更安全的变体)来执行此操作; but since you are using C++ , you can do better, and write easier code: just use a C++ string class , like std::string . 但是由于您使用的是C ++ ,因此您可以做得更好,编写更简单的代码:只需使用C ++字符串类 ,例如std::string

In fact, the C++ standard library offers convenient overloads for operator+ that work with std::string , so you can concatenate C++ strings in an easy, intuitive and safe way; 实际上,C ++标准库为使用std::string operator+提供了方便的重载,因此您可以通过简单,直观和安全的方式连接C ++字符串。 for example: 例如:

// Build your URL string
std::string test = decode(foo[5]);
std::string url = test + "filename.txt";

// Use std::string::c_str() to convert from C++ string
// to C raw string pointer const char*
downloadFile(url.c_str(), "/user/tmp/file.txt");

暂无
暂无

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

相关问题 错误无法转换 &#39;std::string {aka std::basic_string<char> }&#39; 到 &#39;char&#39; 赋值 - C++ - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment - C++ C ++ FindFirstFile无法将const char转换为basic_string - c++ FindFirstFile Cannot convert const char to basic_string 错误:无法转换 'std::basic_string<char> ::iterator...' to 'const char* for argument '1'...'</char> - error: cannot convert 'std::basic_string<char>::iterator ...' to 'const char* for argument '1' ...' 错误:无法转换 &#39;std::basic_string<char> &#39; 到 &#39;char&#39; 赋值 - error: cannot convert 'std::basic_string<char>' to 'char' in assignment 无法转换 &#39;std::basic_string<char> &#39; 到 &#39;const char*&#39; 作为参数 &#39;1&#39; 到 &#39;int system(const char*)&#39; - cannot convert 'std::basic_string<char>' to 'const char*' for argument '1' to 'int system(const char*)' C++“错误:传递&#39;const std::map <int, std::basic_string<char> &gt;&#39; 作为 &#39;this&#39; 的论点...&quot; - C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..." OSX“错误:无法转换&#39;const std :: __ cxx11 :: basic_string”是什么意思 <char> “ 意思? - OSX What does “error: cannot convert 'const std::__cxx11::basic_string<char>” mean? 错误:无法转换&#39;std :: basic_string <char> 从&#39;到&#39;int&#39;分配 - error: cannot convert ‘std::basic_string<char>’ to ‘int’ in assignment 错误:无法转换&#39;std :: string {aka std :: basic_string <char> 初始化时&#39;&#39;到&#39;char *&#39; - error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ in initialization 错误:无法匹配'(const std :: basic_string <char>)()' - error: no match for call to '(const std::basic_string<char>) ()'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM