저는 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;
}
당신은 그 중에서도 – lpapp
.... 실제 오류 메시지를 보여 주었다하지 않은 , 가변 길이 배열 (C++에는 존재하지 않음)을 사용하고 있습니다. 그건 그렇고, 이것은 C++보다 훨씬 더 C 스타일의 코드입니다. 또한 오류 메시지를보고 최소한 생각하고 /하거나 검색하여 오류를 해결하려고 시도해야합니다. –
첫 번째 오류로 시작하십시오. –