const int fileLength = fileContent.length();
char test[1000];
for (int p = 0; p < fileLength; ++p){
test[p].append(fileContent[p]); // Error: expression must have class type
};
Run Code Online (Sandbox Code Playgroud)
我试图将文本文件的字符附加到我创建的数组中.虽然我收到错误"表达式必须具有类类型".尝试谷歌搜索此错误无济于事.
test是一个char数组. test[p]是一个炭. char没有任何成员.特别是,它没有append成员.
你可能想做一个测试 std::vector<char>
const auto fileLength = fileContent.length();
std::vector<char> test;
for (const auto ch : fileContent)
{
test.push_back(ch);
}
Run Code Online (Sandbox Code Playgroud)
甚至:
std::vector<char> test( fileContent.begin(), fileContent.end() );
Run Code Online (Sandbox Code Playgroud)
如果你真的需要将其test视为一个数组(例如,因为你正在连接一些C函数),那么使用:
char* test_pointer = &*test.begin();
Run Code Online (Sandbox Code Playgroud)
如果你想将它用作以空字符结尾的字符串,那么你应该使用std :: string代替,并获取指针test.c_str().