2013-10-18 3 views
0

그래서 자동을 사용하는 프로그램을 작성했지만 컴파일러가 인식하지 못하는 것 같습니다. 아마도 이전 컴파일러 일 것입니다.컴파일러가 자동으로 인식하지 못하는 것은 무엇일까요?

내 코드가 궁금 해서요. 자동 키워드를 사용할 필요가 없도록 내 코드를 수정하는 데 적합한 변수는 무엇입니까? 문자열에 대한 포인터를 생각하고 있습니까? 또는 문자열 반복기입니다.

#include <cstdlib> 
    #include <string> 
    #include <iostream> 
    #include <unistd.h> 
    #include <algorithm> 
    using namespace std; 

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

     enum MODE { 
      WHOLE, PREFIX, SUFFIX, ANYWHERE, EMBEDDED 
     } mode = WHOLE; 
     bool reverse_match = false; 

     int c; 
     while ((c = getopt(argc, argv, ":wpsaev")) != -1) { 
      switch (c) { 
       case 'w': // pattern matches whole word 
        mode = WHOLE; 
        break; 
       case 'p': // pattern matches prefix 
        mode = PREFIX; 
        break; 
       case 'a': // pattern matches anywhere 
        mode = ANYWHERE; 
        break; 
       case 's': // pattern matches suffix 
        mode = SUFFIX; 
        break; 
       case 'e': // pattern matches anywhere 
        mode = EMBEDDED; 
        break; 
       case 'v': // reverse sense of match 
        reverse_match = true; 
        break; 
      } 
     } 
     argc -= optind; 
     argv += optind; 

     string pattern = argv[0]; 


     string word; 
     int matches = 0; 

     while (cin >> word) { 

      switch (mode) { 
       case WHOLE: 
        if (reverse_match) { 
         if (pattern != word) { 
          matches += 1; 
          cout << word << endl; 
         } 
        } else if (pattern == word) { 
         matches += 1; 
         cout << word << endl; 
        } 
        break; 
       case PREFIX: 
        if (pattern.size() <= word.size()) { 
         auto res = mismatch(pattern.begin(), pattern.end(), word.begin()); 

         if (reverse_match) { 
          if (res.first != word.end()) { 
           matches += 1; 
           cout << word << endl; 
          } 
         } else if (res.first == word.end()) { 
          matches += 1; 
          cout << word << endl; 
         } 
        } 
        break; 
       case ANYWHERE: 

        if (reverse_match) { 
         if (!word.find(pattern) != string::npos) { 
          matches += 1; 
          cout << word << endl; 
         } 
        } else if (word.find(pattern) != string::npos) { 
         matches += 1; 
         cout << word << endl; 
        } 
        break; 
       case SUFFIX: 
        if (pattern.size() <= word.size()) { 
         auto res = mismatch(pattern.rbegin(), pattern.rend(), word.rbegin()); 

         if (reverse_match) { 
          if (res.first != word.rend()) { 
           matches = +1; 
           cout << word << endl; 
          } 
         } else if (res.first == word.rend()) { 
          matches = +1; 
          cout << word << endl; 
         } 
        } 
        break; 
       case EMBEDDED: 
        if (reverse_match) { 
         if (!pattern.find(word) != string::npos) { 
          matches += 1; 
          cout << word << endl;} 

        } else if (pattern.find(word) != string::npos) { 
         matches += 1; 
         cout 

<< word << endl; 
       } 
       break; 
     } 

    } 
    return (matches == 0) ? 1 : 0; 
} 

미리 감사드립니다.

오류 내가 얻을 :

main.cpp:70:26: error: 'res' does not name a type 
main.cpp:73:29: error: 'res' was not declared in this scope 
main.cpp:77:32: error: 'res' was not declared in this scope 
main.cpp:97:26: error: 'res' does not name a type 
main.cpp:100:29: error: 'res' was not declared in this scope 
main.cpp:104:32: error: 'res' was not declared in this scope 
+0

"컴파일러가 인식하지 못하는 것"을 정의합니다. 방법? 당신이 기대하는 것은 무엇입니까? 어느 선 이요? – Adam

+2

어떤 컴파일러입니까? – chris

+1

하나의 사이즈로 대체 할 수있는 것은 하나도 없지만, 하나는'std :: pair '이고 다른 하나는'std :: pair '그러나 나는 왜'auto'가 그 일을하지 않는지 더 궁금합니다. 그래서 두 번째 chris '질문. ** 어떤 컴파일러를 사용하고 있습니까? ** – WhozCraig

답변

1

당신이 불일치의 반환 값을 저장하는 변수를 선언 할 자동차를 사용하고 있기 때문에, 그것은 말했다 함수의 반환 형식으로 대체 할 수있다. this에 따르면, 문제는이 의미하는 것 처음 사용에서

std::pair<InputIt1,InputIt2>. 

을 것 InputIt1 =이 InputIt2 =는 표준 : : 문자열 :: 반복자 (표준의 반환 유형 : 문자열 :: 시작) 및 결과 유형은해야 이 반환 표준 : : 문자열의 유형 :: rbegin() 될 두 번째에서 표준 : : 쌍

일 :이 도움이되기를 바랍니다

std::pair<std::string::reverse_iterator,std::string::reverse_iterator> 

하지만 물론 오히려 지루 그런 긴 타이프 네임을 입력하면 자동으로, imho가 선호됩니다. 보다 현대적인 컴파일러 (또는 현재 플래그 (예 : g ++의 -std = C++ 11)에 올바른 플래그를 전달), 그렇게하는 것이 좋습니다.