2016-09-12 21 views
1

私は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; 
} 
+1

あなたは[] ''のstdと '演算子を使用することはできません。 :list'。たぶん、あなたは 'std :: vector'を試してみたい、あるいはwhileの代わりにイテレータに基づいて' for'を使いたいかもしれません。 – tforgione

+0

'std :: list'から[' std :: vector'](http://en.cppreference.com/w/cpp/container/vector)に切り替えます。 'std :: vector'は常にデフォルトのコンテナです。リストはここで意味をなさない、あなたが気づいたように、あなたはリストでサブスクリプションを使用することはできません。 –

+1

また、配列と同様に、ベクターはゼロでインデックスを開始します。 –

答えて

1

あなたはiteratorを使用することができます。 iteratorは、STLコンテナ内の要素へのポインタに似ています。

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

あなたは近代的なコンパイラを持っている場合は、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; 
} 
+0

と 'return ...;'についてはどうですか? –

+1

良い質問です。この場合、C++は0を返しますが、私はそのようにデフォルトすることに賛成ではありません。あなたは戻り値を必要としないので、関数が何かを返すのはなぜですか? 'void'はより良い型になります。私はそれを編集します。 – nvoigt

+0

私はあなたのコードを 'main()'関数と 'commands_help'に直接' ' 'は有効な範囲の型ではありません"と言っていましたが、commands_helpの各項目に "excess in scalar initializer" –

関連する問題