1

I have the following code that tries to enumerate strings.

#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;
                   }
              }
          }
      }
   }
}

But how come upon compilation it gave error:

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
1
  • I don't want to be harsh, but this is why learning dynamic languages first is wrong. The starting point of a program must be a routine, be it explicit (as in C) or implicit (as in, say, Python). Commented Mar 26, 2009 at 2:04

3 Answers 3

5

The error is actually in the following line, at the for loop: your code needs to be contained in a function of some sort, most likely int main(void)

Sign up to request clarification or add additional context in comments.

Comments

3

You are missing a main.

Try:

#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

Comments

2

I see 2 main problems right off the bat.

1) You have no main() and no return code for it, rightfully so.

2) countof() does not exist, you are probably looking for 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;
}

3 Comments

Also sizeof would give the wrong answer there. You would want "sizeof(values)/sizeof(values[0])" minimally.
Ah i didn't even notice he was checking size of the array, I'll add in the macro.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.