简体   繁体   English

错误:无法从&#39;std :: string * {aka std :: basic_string转换 <char> *}&#39;为&#39;std :: string {aka std :: basic_string <char> }&#39;|

[英]error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'|

I'm trying to make a function that writes a file but i'm having issues passing a string as a parameter. 我正在尝试制作一个写入文件的函数,但在传递字符串作为参数时遇到问题。

void writeFile(string filename, string letters, int size)
{
     ofstream outputfile("output.txt");
     outputfile << letters;
     outputfile.close();

 }

int main()
{
    string letters[] = {"u", "l", "s", "n","m", "z", "a", "p", "b"};

    int size = 9;

    string filename = "Inputfile.txt";

    writeFile(inputfilename.c_str(),letters,size);

}

And having this error. 并且有这个错误。

error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'|

An array of string items is passed as actual argument, where the formal argument is a single string . string项数组作为实际参数传递,其中形式参数是单个string

You could replace the array with a single string. 您可以用单个字符串替换数组。

Or with a set, or whatever suits the purpose, but then you'll have to change the called function accordingly. 或带有集合,或任何适合其用途的东西,但随后必须相应地更改被调用的函数。


The error mentions std::string* instead of an array, like std::string[9] , because the array expression decays to an expression denoting a pointer to the first item, which it does because the array is not being bound to a reference or passed to sizeof or anything where the expression's array nature would have to be preserved. 错误提到的是std::string*而不是数组,例如std::string[9] ,因为数组表达式会衰减为表示指向第一个项目的指针的表达式,因为数组未绑定到a引用或传递给sizeof或必须保留表达式的数组性质的任何内容。

The problem was that 问题是

void writeFile(string filename, string letters, int size)

is expecting a string but, 正在期待一个字符串,但是,

string letters[] = {"u", "l", "s", "n","m", "z", "a", "p", "b"};

is an array of string, i "fixed" it by changing the function second parameter to 是一个字符串数组,我通过将函数的第二个参数更改为“固定”它

void writeFile(string outputFileName, string words[], int arraylength)

with "fixed" i meant that regardless of that now it does creates a file named "output.txt" but the content is not the array of string passed as a second parameter. “固定”是指无论现在它确实创建了一个名为“ output.txt”的文件,但内容不是作为第二个参数传递的字符串数组。

暂无
暂无

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

相关问题 std :: string {aka std :: basic_string <char> 分配给&#39;&#39;&#39;char *&#39; - std::string {aka std::basic_string<char>}' to 'char*' in assignment| 错误:与“ operator *”不匹配(操作数类型为“ std::string {aka std basic_string <char> }&#39;和{aka std basic_string <char> }&#39;) - error: no match for 'operator*' (operand types are 'std: :string {aka std basic_string<char>}' and {aka std basic_string<char>}') 错误:无法转换&#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 错误无法转换 &#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++ 错误无法转换 'std::string {aka std::basic_string<char> }' 到 'int' 赋值</char> - Error cannot convert 'std::string {aka std::basic_string<char>}' to 'int' in assignment 错误无法转换 'std::string* {aka std::basic_string<char> *}' 到 'node*' 在分配</char> - Error cannot convert 'std::string* {aka std::basic_string<char>*}' to 'node*' in assignment C ++错误:无法调用&#39;(std :: string {aka std :: basic_string <char> })(std :: string&)&#39; - C++ Error: no match for call to ‘(std::string {aka std::basic_string<char>}) (std::string&)’ 无法转换&#39;std :: string {aka std :: basic_string <char> }&#39;为&#39;char&#39;作为回报 - Cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char’ in return 错误:不匹配调用 (std::string{aka std::basic_string<char> })(const char[5])&#39; - Error: no match for call to (std::string{aka std::basic_string<char>})(const char[5])' 错误:为&#39;operator std::string {aka std::__cxx11::basic_string 指定的返回类型<char> }&#39; - Error:return type specified for 'operator std::string {aka std::__cxx11::basic_string<char>}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM