title: Not declared in this scope
body: <p>I'm in the process of implementing a MIPS instruction set simulator. But when I try compiling my code, I keep getting "out of scope" errors and it's getting to be very frustrating. I've tried all kinds of variations, but nothing seems to be helping.<br>
  Here's my MIPSsim.cpp file  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cassert&gt;
  
  using namespace std;
  const int BITS = 32;
  
  string inst_decode(string line);
  void execute(string line, string instruction, int &amp;registers[]);
  void print(int cycle, int inst_address, int regs[]/*, int data[]*/);
  int convert_5(string binary);
  int convert_32(string binary);
  void add(string line, int &amp;registers[]);
  void sub(string line, int &amp;registers[]);
  void mult(string line, int &amp;registers[]);
  
  
  string inst_decode(string line){
      string instruction; 
      if(line.compare(0,1,"0") == 0){
          if (line.compare(1,5,"00010") == 0){
              cout &lt;&lt; "JUMP" &lt;&lt; endl;
              instruction = "JUMP";
          }else if (line.compare(1,5,"00100") == 0){
          cout &lt;&lt; "BEQ" &lt;&lt; endl;
          instruction = "BEQ";
      }else if (line.compare(1,5,"00001") == 0){
          cout &lt;&lt; "BLTZ" &lt;&lt; endl;
          instruction = "BLTZ";
      }else if (line.compare(1,5,"00111") == 0){
          cout &lt;&lt; "BGTZ" &lt;&lt; endl;
          instruction = "BGTZ";
      }else if (line.compare(1,5,"01011") == 0){
          cout &lt;&lt; "SW" &lt;&lt; endl;
          instruction = "SW";
      }else if (line.compare(1,5,"00011") == 0){
          cout &lt;&lt; "LW" &lt;&lt; endl;
          instruction = "LW";
      }else if (line.compare(1,5,"00010") == 0){
          cout &lt;&lt; "SRL" &lt;&lt; endl;
          instruction = "SRL";
      }else if ((line.compare(1,5,"11100") == 0)&amp;&amp;(line.compare(26,6,"000010") == 0)){
          cout &lt;&lt; "MUL" &lt;&lt; endl;
          instruction = "MUL";
      }else if (line.compare(1,5,"00000") == 0){
          if (line.compare(26,6,"100000") == 0){
              cout &lt;&lt; "ADD" &lt;&lt; endl;
              instruction = "ADD";
          }else if (line.compare(26,6,"100010") == 0){
              cout &lt;&lt; "SUB" &lt;&lt; endl;
              instruction = "SUB";
          }else if (line.compare(26,6,"100100") == 0){
              cout &lt;&lt; "AND" &lt;&lt; endl;
              instruction = "AND";
          }else if (line.compare(26,6,"100101") == 0){
              cout &lt;&lt; "OR" &lt;&lt; endl;
              instruction = "OR";
          }else if (line.compare(26,6,"101010") == 0){
              cout &lt;&lt; "SLT" &lt;&lt; endl;
              instruction = "SLT";
          }else if (line.compare(26,6,"000000") == 0){
              cout &lt;&lt; "SLL" &lt;&lt; endl;
              instruction = "SLL";
          }else if (line.compare(6,26,"00000000000000000000000000") == 0){
              cout &lt;&lt; "NOP" &lt;&lt; endl;
              instruction = "NOP";
          }
      }
      }else{
          if (line.compare(26,6,"100100") == 0){
              cout &lt;&lt; "ADDI" &lt;&lt; endl;
              instruction = "ADDI";
          }else if (line.compare(26,6,"100010") == 0){
              cout &lt;&lt; "ANDI" &lt;&lt; endl;
              instruction = "ANDI";
          }
      }
      return instruction;
  }
  
  void execute(string line, string instruction, int&amp; registers[]){
      if(instruction == "ADD"){
          add(line, registers);
      }else if(instruction == "ADDI"){
          addi(line, registers);
      }else if(instruction == "SUB"){
          sub(line, registers);
      }else if(instruction == "MUL"){
          mult(line, registers);
      }
  }
  
  void print(int cycle, int inst_address, int regs[]/*, int data[][]*/){
      cout &lt;&lt; "---------------------------\n";
      cout &lt;&lt; "Cycle: " &lt;&lt; cycle &lt;&lt; "\t" &lt;&lt; inst_address &lt;&lt; "\t" &lt;&lt; "\n";
      cout &lt;&lt; "\nRegisters\n";
      cout &lt;&lt; "R00:\t";
      for(int i=0; i&lt;=15; i++){
          cout &lt;&lt; regs[i] &lt;&lt; "\t";
      }
      cout &lt;&lt; "\nR16:\t";
      for(int i=16; i&lt;32; i++){
              cout &lt;&lt; regs[i] &lt;&lt; "\t";
      }
      cout &lt;&lt; "\n\nData\n";
          //cout &lt;&lt; "\n148:\t";
          //for(int i=0; i&lt;8; i++){
          //  cout &lt;&lt; data[i] &lt;&lt; "\t";
          //}
          //cout &lt;&lt; "\n180:\t";
          //for(int i=9; i&lt;17; i++){
              //  cout &lt;&lt; data[i] &lt;&lt; "\t";
          //}
          //cout &lt;&lt; "\n212:\t";
          //for(int i=18; i&lt;26; i++){
          //  cout &lt;&lt; data[i] &lt;&lt; "\t";
      //}
      cout &lt;&lt; "\n";
  }
  
  
  int main(){
      int registers[32] = {0};
          //  int data[2][32];
      string inst;
      //int cycle = 1;
      //int PC = 64;
      string binary_inst[64];
  
      ifstream inFile;
      inFile.open("sample.txt");
      if (!inFile) {
          cout &lt;&lt; "Unable to open file sample.txt" &lt;&lt; endl;
          return 0;
      }
      string x;
      while(!inFile.eof()){
          for(int i=0; i&lt;64; i++){
              inFile &gt;&gt; x;
              binary_inst[i] = x;
              inst = inst_decode(binary_inst[i]);
              execute(binary_inst[i], inst, registers);
              //print(cycle, PC, registers);
          }
      } 
      //  print(cycle, PC, registers);
  
      return 0;
  }
  
  
  
  int convert_5(string binary) {
      long power = 32;
      long sum = 0;
      assert(binary.size() == 5);
  
      for ( int i=0; i&lt;BITS; ++i) {
          if ( i==0 &amp;&amp; binary[i]!='0') {
              sum = -32;
          }
          else {
              sum += (binary[i]-'0')*power;
          }
          power /= 2;
      }
      return sum;
  }
  
  int convert_32(string binary) {
      long power = 4294967296;
      long sum = 0;
      assert(binary.size() == 32);
  
      for ( int i=0; i&lt;BITS; ++i) {
          if ( i==0 &amp;&amp; binary[i]!='0') {
              sum = -4294967296;
          }
          else {
              sum += (binary[i]-'0')*power;
          }
          power /= 2;
      }
      return sum;
  }
  
  
  
  void add(string line, int&amp; registers[]){
      int dest = convert_5(line.substr(17,5));
      cout &lt;&lt; "Dest: " &lt;&lt; dest &lt;&lt; endl;
  
      int target = convert_5(line.substr(12,5));
      cout &lt;&lt; "Target: " &lt;&lt; target &lt;&lt; endl;
  
      int source = convert_5(line.substr(7,5));
      cout &lt;&lt; "Source: " &lt;&lt; source &lt;&lt; endl;
  
      registers[dest] = registers[source] + registers[target];
      cout &lt;&lt; "Addition: " &lt;&lt; registers[dest] &lt;&lt; endl;
  }
  
  void sub(string line, int&amp; registers[]){
      int dest = convert_5(line.substr(17,5));
      cout &lt;&lt; "Dest: " &lt;&lt; dest &lt;&lt; endl;
  
      int target = convert_5(line.substr(12,5));
      cout &lt;&lt; "Target: " &lt;&lt; target &lt;&lt; endl;
  
      int source = convert_5(line.substr(7,5));
      cout &lt;&lt; "Source: " &lt;&lt; source &lt;&lt; endl;
  
      registers[dest] = registers[source] - registers[target];
      cout &lt;&lt; "Subtraction: " &lt;&lt; registers[dest] &lt;&lt; endl;
  }
  
  void mult(string line, int&amp; registers[]){
      int dest = convert_5(line.substr(17,5));
      cout &lt;&lt; "Dest: " &lt;&lt; dest &lt;&lt; endl;
  
      int target = convert_5(line.substr(12,5));
      cout &lt;&lt; "Target: " &lt;&lt; target &lt;&lt; endl;
  
      int source = convert_5(line.substr(7,5));
      cout &lt;&lt; "Source: " &lt;&lt; source &lt;&lt; endl;
  
      registers[dest] = registers[source] * registers[target];
      cout &lt;&lt; "Multiplication: " &lt;&lt; registers[dest] &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Here are the error messages I keep getting:  </p>
  
  <pre><code>lin114-08:10% make all
  g++ -Wall -o MIPSsim MIPSsim.cpp
  MIPSsim.cpp:10: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp:14: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp:15: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp:16: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp:85: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp: In function ‘void execute(...)’:
  MIPSsim.cpp:86: error: ‘instruction’ was not declared in this scope
  MIPSsim.cpp:87: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:87: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp:89: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:89: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp:89: error: ‘addi’ was not declared in this scope
  MIPSsim.cpp:91: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:91: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp:93: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:93: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp: In function ‘int main()’:
  MIPSsim.cpp:146: warning: cannot pass objects of non-POD type ‘struct std::string’     through ‘...’; call will abort at runtime
  MIPSsim.cpp:146: warning: cannot pass objects of non-POD type ‘struct std::string’     through ‘...’; call will abort at runtime
  MIPSsim.cpp: At global scope:
  MIPSsim.cpp:193: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp: In function ‘void add(...)’:
  MIPSsim.cpp:194: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:203: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp: At global scope:
  MIPSsim.cpp:207: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp: In function ‘void sub(...)’:
  MIPSsim.cpp:208: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:217: error: ‘registers’ was not declared in this scope
  MIPSsim.cpp: At global scope:
  MIPSsim.cpp:221: error: declaration of ‘registers’ as array of references
  MIPSsim.cpp: In function ‘void mult(...)’:
  MIPSsim.cpp:222: error: ‘line’ was not declared in this scope
  MIPSsim.cpp:231: error: ‘registers’ was not declared in this scope
  make: *** [MIPSsim] Error 1
  </code></pre>
  
  <p>Any help would be greatly appreciated</p>