2014-04-27 3 views
-3

저는 C++로 가상 머신을 작성하고 있으며 Clang에서 컴파일하지만 GCC에서 컴파일 할 때 많은 오류가 발생합니다. 아무도 그 이유를 말할 수 있습니까? 내 컴파일러에서 코드가 어떻게 컴파일되는지는 모르지만 다른 컴파일러에서는 컴파일되지 않습니다. 그들은 이제 같아야합니까?내 C++은 Clang에서 컴파일되지만 GCC에서는 컴파일되지 않습니다.

#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 

#define OP_EOI 0 
#define OP_EOP 1 
#define OP_PUSH 2 
#define OP_POP 3 
#define OP_PRINT 4 
#define OP_ADD 5 
#define OP_MUL 6 
#define OP_SUB 7 

using namespace std; 

class reedoovm { 

    private: 
     string filedata; 
     string instruction; 
     string file; 
     int instr; 
     int instructionCount; 
     int instructionPointer; 
     int stack; 

    public: 
     string load_program(string filename) { 
      ifstream rdfile(filename); 
      while(rdfile >> instruction) {   /* Get each instruction */ 
        filedata += instruction;  /* Append the instruction to filedata */ 
        filedata += ",";    /* Append a comma to separate each instruction */ 
        instructionCount++; 
       } 
      rdfile.close();       /* Close the file */ 
      return filedata;      /* Return the filedata */ 
     } 

     int *instrToArr(string file) { 
      stringstream hextoint; 
      unsigned int value; 
      string s = file;      /* store fconv in a variable "s" */ 
      string delimiter = ",";     /* The delimiter */ 
      size_t pos = 0; 
      string token; 
      int i = 0; 
      int inst; 
      static int* instarray; 
      instarray = (int*) calloc(instructionCount,sizeof(int)); 
      while ((pos = s.find(delimiter)) != string::npos) {  /* Convert hex instructions to decimal */ 
       token = s.substr(0, pos); 
       stringstream hextoint(token); 
       hextoint >> hex >> value; 
       if (i < instructionCount) { 
        instarray[i] = value; 
        i++; 
       } 
       s.erase(0, pos + delimiter.length()); 
      } 
      return instarray; 
     } 

     int * instructionArray(int instructionArray[]) { 
      return instructionArray; 
     } 

     int getNextIntruction(int instructions[], int i) { 
      return instructions[i]; 
     } 

     void do_PRINT() { 

     } 

     void do_PUSH(int instructions, int i) { 
      //cout << instructions[i + 1] << endl; 
     } 

     void run_program(int instructions[], string file) { 
      int loop = 1; 
      int i = 0; 
      string delimiter = ",";     /* The delimiter */ 
      size_t pos = 0; 
      string token; 
      int iterator = 0; 
      instructionCount = count(file.begin(), file.end(), ','); 
      int instructionOrLiteralArray[instructionCount]; 
      while ((pos = file.find(delimiter)) != string::npos) {  /* Convert hex instructions to decimal */ 
       token = file.substr(0, pos); 
       if (token.length() == 2) {        /* Operation */ 
        instructionOrLiteralArray[iterator] = 0; 
       } else { 
        instructionOrLiteralArray[iterator] = 1;   /* Literal */ 
       } 
       iterator++; 
       file.erase(0, pos + delimiter.length()); 
      } 
      while (loop) { 

       instr = getNextIntruction(instructions, i); 

       if (instr == OP_EOI && instructionOrLiteralArray[i] == 0) { 
        cout << "EOI" << endl; 
       } else if (instr == OP_EOI && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (instr == OP_PUSH && instructionOrLiteralArray[i] == 0) { 
        do_PUSH(instr, i); 
       } else if (instr == OP_PUSH && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (instr == OP_PRINT && instructionOrLiteralArray[i] == 0) { 
        do_PRINT(); 
       } else if (instr == OP_PRINT && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (instr == OP_POP && instructionOrLiteralArray[i] == 0) { 
        cout << "POP" << endl; 
       } else if (instr == OP_POP && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (instr == OP_ADD && instructionOrLiteralArray[i] == 0) { 
        cout << "ADD" << endl; 
       } else if (instr == OP_ADD && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (instr == OP_SUB && instructionOrLiteralArray[i] == 0) { 
        cout << "MUL" << endl; 
       } else if (instr == OP_MUL && instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       else if (instructionOrLiteralArray[i] == 1) { 
        cout << "Literal" << endl; 
       } 

       if (i < instructionCount) { 
        i++; 
       } else { 
        loop = 0; 
       } 
      } 
     } 

     void execute_program(string s) { 
      file = load_program(s); 
      int * arr = instrToArr(file); 
      int * instructions = instructionArray(arr); 
      run_program(instructions, file); 
     }  

}; 

int main(int argc, char* argv[]) { 
    reedoovm rd; 
    rd.execute_program(argv[1]); 
    return 0; 
} 
+2

당신은 그 중에서도 – lpapp

+0

.... 실제 오류 메시지를 보여 주었다하지 않은 , 가변 길이 배열 (C++에는 존재하지 않음)을 사용하고 있습니다. 그건 그렇고, 이것은 C++보다 훨씬 더 C 스타일의 코드입니다. 또한 오류 메시지를보고 최소한 생각하고 /하거나 검색하여 오류를 해결하려고 시도해야합니다. –

+0

첫 번째 오류로 시작하십시오. –

답변

3

당신은 표준 : 계산을위한 알고리즘이 포함되지 않습니다

여기에 코드입니다.

instructionCount = count(file.begin(), file.end(), ','); 

나는 그것이 실제로는 안되기 때문에 clang으로 빌드된다는 것에 실제로 놀랐습니다. 그리고 그것은 나를 위해 컴파일되지 않습니다.

내부 암시 적 포함 정리와 관련이있을 수 있습니다. gcc 변종이 포함되어 있기 때문에 일부 포함은 암시 적이며 개발자에게는 실제로 명시 적으로 의존하고 있습니다. 이것은 좋은 습관이 아닙니다. 항상 사용하는 것을 명시 적으로 포함하십시오.

당신은 상단에이를 수정해야합니다 :

ifstream rdfile(filename); 

그것은 당신이 컴파일러의 -std=c++11 옵션을 사용해야합니다 의미 :이 라인을 쓸 때 참고, 또한

#include <algorithm> 

std :: string을 허용하는 ifstream의 생성자 유형이 C++ 11에서만 도입 되었기 때문에 gcc 또는 clang으로 지정하십시오.

이전 버전의 경우 파일의 char * 이름을 얻으려면 filenamefilename.c_str()으로 변경해야합니다.

따라서이 내가 여분의 명시를 추가 한 후 컴파일 코드를 제작하는 방법입니다은 다음과 같습니다

g++ -std=c++11 main.cpp 

clang -std=c++11 main.cpp 
+0

당신의 도움에 감사드립니다! – user3566150

관련 문제