2009-03-26 13 views
1

文字列を列挙しようとする次のコードがあります。ハウツー文字配列を構成する

#include <string> 
#include <iostream> 

using namespace std; 

string base = "000"; 
char values[] = {'0', '1', '2', '3' }; // Error Here 

for (int i = 0; i < base.length(); ++i) 
{ 
    for (int j = 0; j < countof(values); ++j) 
    { 
     if (base[i] != values[j]) 
     { 
      string copy = base; 
      copy[i] = values[j]; 
      cout << copy << endl; 

      for (int k = i+1; k < base.length(); ++k) 
      { 
       for (int l = 0; l < countof(values); ++l) 
       { 
        if (copy[k] != values[l]) 
        { 
         string copy2 = copy; 
         copy[k] = values[l]; 
         cout << copy2 << endl; 
        } 
       } 
      } 
     } 
    } 
} 

しかし、どのようにコンパイル時に来る、それはエラーを与えた:

test.cc:9: error: expected unqualified-id before 'for' 
test.cc:9: error: expected constructor, destructor, or type conversion before '<' token 
test.cc:9: error: expected unqualified-id before '++' token 
+0

私は厳しいものではありませんが、これは動的言語の学習が最初に間違っている理由です。プログラムの開始点は、明示的(Cのように)または暗黙的(例えば、Pythonのように)でなければなりません。 – pyon

答えて

5

エラーforループで、次の行に実際にある:あなたのコードは、ある種の機能に含まれる必要があります、おそらくint main(void)

3

あなたはメインがありません。

試してみてください。

#include <string> 
#include <iostream> 

using namespace std; 

string base = "000"; 
char values[] = {'0', '1', '2', '3' }; // Error Here 

int main() // Added 
{ // Added 

    for (int i = 0; i < base.length(); ++i) 
    { 
     for (int j = 0; j < countof(values); ++j) 
     { 
     if (base[i] != values[j]) 
     { 
      string copy = base; 
      copy[i] = values[j]; 
      cout << copy << endl; 

      for (int k = i+1; k < base.length(); ++k) 
      { 
       for (int l = 0; l < countof(values); ++l) 
       { 
         if (copy[k] != values[l]) 
         { 
          string copy2 = copy; 
          copy[k] = values[l]; 
          cout << copy2 << endl; 
         } 
       } 
      } 
     } 
     } 
    } 

    return 0; // Added 
} // Added 
2

私は右のバット2つの主な問題を参照してください。

1)main()と戻りコードがありません。正当な理由があります。

2)countof()が存在しないため、おそらくsizeof()を探している可能性があります。

#include <string> 
#include <iostream> 
#define countof(array) (sizeof(array)/sizeof(array[0])) 

using namespace std; 

int main(int argc, char *argv[]) { 

string base = "000"; 
char values[] = {'0', '1', '2', '3' }; // Error Here 

    for (int i = 0; i < base.length(); ++i) 
    { 
     for (int j = 0; j < countof(values); ++j) 
     { 
      if (base[i] != values[j]) 
      { 
       string copy = base; 
       copy[i] = values[j]; 
       cout << copy << endl; 

       for (int k = i+1; k < base.length(); ++k) 
       { 
        for (int l = 0; l < countof(values); ++l) 
        { 
         if (copy[k] != values[l]) 
         { 
          string copy2 = copy; 
          copy[k] = values[l]; 
          cout << copy2 << endl; 
         } 
        } 
       } 
      } 
     } 
    } 
return 0; 
} 
+0

たぶん彼はこれを使っています:http://blogs.msdn.com/the1/archive/2004/05/07/128242.aspx – grieve

+0

また、sizeofは間違った答えを与えます。あなたは "sizeof(values)/ sizeof(values [0])"を最小限にしたいでしょう。 – grieve

+0

ああ、私は彼が配列のサイズを調べていることに気付かず、マクロに追加します。 –

関連する問題