Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
追加到数组中的C++_C++_Append - Fatal编程技术网

追加到数组中的C++

追加到数组中的C++,c++,append,C++,Append,我试图将文本文件的字符附加到我创建的数组中。虽然我得到了错误表达式必须有类类型。尝试用谷歌搜索此错误,但无效。测试是一个字符数组。test[p]是一个字符。char没有任何成员。特别是,它没有附加成员 您可能希望将测试设置为std::vector 如果要将其用作以nul结尾的字符串,则可能应该改用std::string,并使用test.c_str获取指针。char数组没有任何名为append的成员函数。但是,std::string有一个名为append的成员函数,如下所示: char*

我试图将文本文件的字符附加到我创建的数组中。虽然我得到了错误表达式必须有类类型。尝试用谷歌搜索此错误,但无效。

测试是一个字符数组。test[p]是一个字符。char没有任何成员。特别是,它没有附加成员

您可能希望将测试设置为std::vector


如果要将其用作以nul结尾的字符串,则可能应该改用std::string,并使用test.c_str获取指针。

char数组没有任何名为append的成员函数。但是,std::string有一个名为append的成员函数,如下所示:

    char* test_pointer = &*test.begin();
string& append (const char* s, size_t n);
我认为您错误地使用了char数组而不是std::string。 std::string将解决此问题,如下所示:

    char* test_pointer = &*test.begin();
string& append (const char* s, size_t n);

更好的方法是字符串testfileContent。您可以像访问数组一样访问测试。有关更多详细信息,请参阅字符串类。

如果要使用“附加”,为什么要使用字符数组而不是字符串或向量?@LogicStuff:这是针对我的,还是针对OP的?我说可能在几个地方,因为有些情况下这个建议不适用。OP删除了他的评论,我刚才也删除了。
string& append (const char* s, size_t n);
const int fileLength = fileContent.length();
    string test;
    for (int p = 0; p < fileLength; ++p){
        test.append(fileContent[p],1); // Error: expression must have class type
    };