下标值不是数组(错误)
Subscripted value is not an array (error)
我正在用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
。iterator
类似于指向STL容器中元素的指针。例如:
int help() {
list<string>::iterator it = commands_help.begin();
while (it != commands_help.end()){
cout << *it << 'n';
it++;
}
}
如果你有一个现代的编译器,c++ 11已经为你完成了大部分的工作:
#include <vector>
#include <string>
#include <iostream>
std::vector<std::string> commands_help =
{
"'help' ________________ Display this help page.",
"'[command] info' ______ Display command purposes.",
"'datetime' ____________ Can show date, time and calendar.",
"'exit' ________________ Quit the MiSH."
};
void help()
{
for (auto line : commands_help)
{
std::cout << line << std::endl;
}
}
int main()
{
help();
return 0;
}
相关文章:
- "错误:数组下标的无效类型'float [10001][float]'是什么意思?
- 错误:数组下标的类型"const bool[int]"无效
- Qt并发错误:数组初始化需要大括号括起来的初始值设定项列表
- 使用GCC 4.8构建错误:数组用作初始化器
- 如何修复问题中的"teminated due to timeout"错误 数组操作 |黑客兰克?
- | 9 |错误:非静态数据成员'matrix :: row'| 9 |错误:数组绑定不是在']']'令牌|之前的整数常数
- 错误:数组的总大小不得超过0x7fffffff字节
- 错误:无法将参数“1”的“double*”转换为“double”,错误:数组下标的类型“double[double]”无
- 返回并强制转换数组指针(错误:数组下标的类型..无效)
- 返回c++中的字符错误数组
- VC++错误:数组实例化的大小来自模板化的静态常量
- c++mex错误:数组下标的类型“double[mwSize]”无效
- 错误数组用作初始值设定项,我不知道错误
- 编译错误 - 数组下标'char[int]'的类型无效
- 向量在我的类中.错误:数组new [-fpermissive]中的圆括号初始化项
- 错误:数组下标的类型无效
- 错误:数组new中的size必须是整型[-fpermissive]
- 错误:数组下标的“Pixel*[__complex__ int]”类型无效
- c++错误数组显示
- C++:错误:数组下标的类型“size_t{aka long unsigned int}[size_t{aka long