Как построить массив Char - PullRequest
1 голос
/ 26 марта 2009

У меня есть следующий код, который пытается перечислить строки.

#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

Ответы [ 3 ]

5 голосов
/ 26 марта 2009

Ошибка на самом деле в следующей строке, в цикле for: ваш код должен содержаться в некоторой функции, скорее всего int main(void)

3 голосов
/ 26 марта 2009

Вам не хватает основного.

Попытка:

#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 голосов
/ 26 марта 2009

Я сразу вижу две основные проблемы.

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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...