[英]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.