错误:无法从'std :: string * {aka std :: basic_string< char> *}'转换为'std :: string {aka std :: basic_string< char>}}'| [英] error: could not convert from 'std::string* {aka std::basic_string<char>*}' to 'std::string {aka std::basic_string<char>}'|

查看:1108
本文介绍了错误:无法从'std :: string * {aka std :: basic_string< char> *}'转换为'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);

}

并出现此错误.

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

推荐答案

string项的数组作为实际参数传递,其中形式参数是单个string.

An array of string items is passed as actual argument, where the formal argument is a single 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.

该错误提到了std::string*而不是像std::string[9]这样的数组,因为数组表达式 decays 指向一个表示指向第一项的指针的表达式,因为数组不是绑定到引用或传递给sizeof或必须保留表达式的数组性质的任何内容.

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 * {aka std :: basic_string&lt; char&gt; *}'转换为'std :: string {aka std :: basic_string&lt; char&gt;}}'|的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆