gpt4 book ai didi

c++ - 下标值不是数组(错误)

转载 作者:行者123 更新时间:2023-11-30 03:38:37 25 4
gpt4 key购买 nike

我正在用 C++ 编写一些代码。在某一点(第 44 行:cout << commands_help[i];)它说有一个错误:“下标值不是数组”......事实上我使用了一个列表,而不是数组......在函数“help() "我打印列表的每一项 commands_help\n每个项目之间。我该怎么做?

代码:

#include <iostream>
#include <list>
#include <fstream>

using namespace std;

ifstream file;

// variables and arrays
string shell_symbol;

bool get_texture(){
file.open("UsedTexture.txt", ios::in);
if (file.is_open()){
file >> shell_symbol;
file.close();
return true;
} else {
cout << "unable to open file";
file.close();
return false;
}
}


list<string> commands_help = {
"'help' ________________ Display this help page.",
"'[command] info' ______ Display command purposes.",
"'datetime' ____________ Can show date, time and calendar.",
"'exit' ________________ Quit the MiSH."
};

long help_size = commands_help.size();

// functions / commands

int help() {
int i = 1;
commands_help.sort();
while (i < help_size) {
if (i < commands_help.size()){
cout << commands_help[i];
} else {
break;
}
}
}

int main() {
if (get_texture()) {
string inp1;
cout <<
"\nThis is the MiSH, type 'help' or '?' to get a short help.\nType '[command] help' to get a detailed help.\n";
while (true) {
cout << shell_symbol;
cin >> inp1;
if (inp1 == "help" || inp1 == "?") {
help();
} else if (inp1 == "exit") {
break;
} else {

}
}
}
return 0;
}

最佳答案

您可以使用迭代器iterator 就像指向 STL 容器中元素的指针。例如:

int help() {
list<string>::iterator it = commands_help.begin();
while (it != commands_help.end()){
cout << *it << '\n';
it++;
}
}

关于c++ - 下标值不是数组(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39445333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com