2009-03-26 10 views
1

문자열을 열거하려고하는 다음 코드가 있습니다.Howto는 문자 배열을 구성합니다.

#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) 메인() 및 리턴 코드가 없습니다. 정당한 이유가 있습니다.

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

아아아가 배열의 크기를 확인 중이라는 것을조차 알지 못했습니다. 매크로에 추가 할 것입니다. –

관련 문제