Not declared (Even though I declared it)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
using namespace std;

ifstream::pos_type size;
char * memblock;

int main () {
  ofstream asmsource ("asmsource.s");
  if (asmsource.is_open())
  {
    asmsource << " .section .bss\n .lcomm mainarray, 30000 \n .section .rodata\n .section .data\ninoutput:\n .byte 0\n .section .text\n .globl _start\n_start:\n xorl %eax, %eax\n xorl %edi, %edi\n"; //Writes the first part of the source code in. Conventional assembler stuff.
  }
  else return 1;
  ifstream source ("source.bf", ios::in|ios::binary|ios::ate);
  if (source.is_open())
  {
    int size = source.tellg();
    char memsource[size];
    source.seekg (0, ios::beg);
    source.read (memsource, size);
    source.close();
    //I don't need it to be dynamic, since I'm only going to read it.
  }
  else return 1;

  for (int stringpointer = 0; size > stringpointer; stringpointer++) {
    switch (memsource[stringpointer]) {
      case '>':
        asmsource << " call increment\n"; //I put them in functions so they can be set back if they go past zero.
        break;
      case '<':
        asmsource << " call decrement\n"; //Same, only in case they go over 30,000.
        break;
      case '.':
        asmsource << " call print\n";
        break;
      case ',':
        asmsource << " call scan\n";
        break;
      case '+':
        asmsource << " call add\n";
        break;
      case '-':
        asmsource << " call subtract\n";
      default:
      break;
    }

   
  } 
 asmsource.close();
}


brainfuck_compiler_0.1.cpp: In function ‘int main()’:
brainfuck_compiler_0.1.cpp:27: error: ‘memsource’ was not declared in this scope


This is part of the brainfuck compiler I'm writing. I'm getting an error that says I didn't declare the variables, but they've clearly been declared.

Can someone help? Thanks in advance.
Last edited on
It isn't declared. You declared the variable in the scope of an if statement. When the block bound to that if statement ends, the variable is destroyed. It also really doesn't help that you declare it from a variable size, but you can't declare an array from a variable unless it's dynamically allocated.
Last edited on
Actually, putting the for statement in the if statement worked well. ThaNKS.
Topic archived. No new replies allowed.