hello, ive got my code but ive split it up. however i keep getting the following errors:

--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
interpreter.cpp
D:\language systems ica\interpreter.cpp(16) : error C2065: 'Code_table' : undeclared identifier
D:\language systems ica\interpreter.cpp(16) : error C2109: subscript requires array or pointer type
D:\language systems ica\interpreter.cpp(16) : error C2228: left of '.instruction' must have class/struct/union type
parser.cpp
tables.cpp
Error executing cl.exe.

main.exe - 3 error(s), 0 warning(s)

The H file that i want to access:

#ifndef TABLES_H
#define TABLES_H

#include <string>
using std::string;

typedef struct Sym_Table
{
	string Name;
	string type;
	int address;
} Sym_Table;


typedef struct Code_Table
{ 
	int oper_and;
	int instruction_number;
	string instruction;
}  Code_Table;


static int stc = 1;


void add_symbol(string sym, string op_code);
void add_codeTable(string sym , int op_code);
void print_symbol_Table();
void build_code_table(string sym);
void print_code_Table();

int position(string &sym);

#endif

And im trying to access the structure in the following file and the following way:

#include <iostream>
#include <string>

#include "tables.h"
#include "interpreter.h"

using namespace std;

int tos = 1;
int pc =1 ;
int codearray;
int op;

void interpreter (void)
{
	cout << Code_table[pc].instruction;
}

can anyone help? I've heard of extern but im not sure how to use it on structures.

Thanks

John

Look closely:

error C2065: [B]'Code_table' [/B] : undeclared identifier
typedef struct [B]Code_Table[/B]
{ 
	int oper_and;
	int instruction_number;
	string instruction;
}  [B]Code_Table[/B];

You also don't need to use that typedef trick in C++. The compiler does it for you.

Sorry naure i forgot to include the header file...

Code_table[]. ... is correct since ive got an array of structs declared.

Anyway im getting the following errors now:

Linking...
parser.obj : error LNK2005: "struct Code_Table * Code_table" (?Code_table@@3PAUCode_Table@@A) already defined in main.obj
tables.obj : error LNK2005: "struct Code_Table * Code_table" (?Code_table@@3PAUCode_Table@@A) already defined in main.obj
interpreter.obj : error LNK2005: "struct Code_Table * Code_table" (?Code_table@@3PAUCode_Table@@A) already defined in main.obj
Debug/main.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

Thanks...

There's a learning curve in terms of how to effectively use error messages to your advantage.

>>interpreter.cpp(16) : error C2065: 'Code_table' : undeclared identifier

means that you used a variable called Code_table in the cpp file called interpreter that wasn't declared before you tried to use it. Misspelling the variable name is a frequent cause for this type of error report, but there are others as well.

>>error LNK2005: "struct Code_Table * Code_table" already defined in main.obj

likely means that there are two (or more) variables with the name Code_table that are in scope and the linker doesn't know which one two use.

Posting the header files, cpp files and the file containing main() with including all the declarations of Code_table and any functions and function calls that you may send Code_table to as a parameter, but not any of the other stuff in the files would be helpful in trying to figure out what is going on.

right here we go:

main.cpp:

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

using namespace std;

#include "scanner.h"
#include "tables.h"
#include "parser.h"
#include "stack.h"
#include "interpreter.h"

string sym;

int main()
{
	openfile();

	sym = getsym();
	Program(sym);

	print_symbol_Table();

	print_code_Table();


return 0;

}

//scanner doesnt use the tables however the other 3 do so here we go:

this is parser.h

void Program(string &sym)
{
	cout << " **** Start of program ****"<< endl;

	f(sym);
	sym = getsym();
	body_1(sym);
	if(sym == "$")
	{
	add_codeTable(sym , -1);
		cout << "** Parser completed ** " << endl;

		return ;
	}
}

void f(string &sym)
{
	cout << "Function F >> "<< sym << endl;
	declaration(sym);

	if(sym == ";")
	{
		cout << "\nscan completion\n" << endl;
		return;
	}
	else
	return;
}

..

and so on

the header file for this is:

#ifndef PARSER_H
#define PARSER_H

void Error(int Error_Code);
void Program(string &sym);

void f(string &sym);
void declaration(string &sym);
void list(string &sym, string type);
void list_l(string &sym, string type);

void body_1(string &sym);
void body_2(string &sym);
void body_3(string &sym);

bool ch(string &sym);

#endif

//now symbols table itself:

void build_code_table(string sym)
{
	if(sym.find == 2 , "ID")
	{
		Code_table[index].instruction_number = index;
		Code_table[index].instruction = "allocate";
		Code_table[index].oper_and = -1;
		index++;
	}
}

the header file for the code table

#ifndef TABLES_H
#define TABLES_H

#include <string>
using std::string;


typedef struct Code_Table
{ 
	int oper_and;
	int instruction_number;
	string instruction;
}  Code_Table;

typedef struct Sym_Table
{
	string Name;
	string type;
	int address;
} Sym_Table;



Sym_Table Symbols[100];


Code_Table Code_table[100];

static int stc = 1;


void add_symbol(string sym, string op_code);
void add_codeTable(string sym , int op_code);
void print_symbol_Table();
...

and now interpreter

#include <iostream>
#include <string>

#include "interpreter.h"
#include "tables.h"


using namespace std;

int tos = 1;
int pc =1 ;
int codearray;
int op;

void interpreter (void)
{
	cout << Code_table[pc].instruction;
}

and the header file:

#ifndef INTERPRETER_H
#define INTERPRETER_H

void interpreter (void);

#endif

thnaks for your help :D

There are good reasons (ease of reusing code being prominent among them) and there are drawbacks (difficulty debugging being prominent among them) to separating functions into separate header files/libraries. In this case I think you would be well served to start a new project and copy everything from the separate files into a single file. When you get that working, then separate functions that work together into groups and enclose them in a header file/cpp file situation if you want. With the myriad of global variables, incomplete function lists, etc in the code you've posted I find it difficult to say for sure what's happening. My suspicion is that the global variable Code_table that you declare in tables.h is not available to the other header files like parser.h and interpreter.h just like cout isn't available to other files unless you include the iostream header file in the other header files where you want to use cout.

Not that I'm an expert or anything, but I encourage you to avoid global variables whenever possible, and to especially avoid global variables hidden in some header file some place.

This article has been dead for over six months. Start a new discussion instead.