maybe not so easy question :( C++

Soldato
Joined
23 May 2005
Posts
2,964
Location
Auckland, New Zealand
ok need a little help here. I have two source code files with an array of objects (using the STL) declared in the main file and a function in main that says "use the source code in the other source file." I'd like to have a function that operates on the aray of objects inside the other source file. Normally this would just be a case of passing parameters of course. BUT this other source file is ONLY a C file, not C++. It is compiled using a parser generator that only compiles C code.

now, I set up the following files:

parser generated file:
Code:
#include funcs.h

blah blah blah..

function();

function header file:
Code:
#includes for everything i need..

void function();
operation(ObjectArray &array);

function body file (.cpp):
Code:
#include funcs.h

void function(){
      operation(nameofarray);
}

void operation(ObjectArray &array){
      array = ObjectArray(x,y);     //initialise the array
}

main .cpp file:
Code:
#include funcs.h
#includes

/*global space*/

ObjectArray nameofarray;

main{
...
...
Parse(); //uses the parser generated source file

}

but it cannot identify "nameofarray" as it says it not declared yet (function source being compiled before main global declaration?)

compiler returns: funcs.cpp(4) : error C2065: 'nameofarray' : undeclared identifier

Is there any way of passing "nameofarray" into operation(); without passing it from main and through parse (not a possibility, not my function I'm using yacc (yet another compiler compiler)) and through function();? basically keeping all cpp code away from the parser generated file?

cheers for any help :)
 
Last edited:
spelling mistake :P its not a copy, its just very pseudo to what i have :p

I'm not sure how include file heirarchy works.. would it be possible to have a file that declared the array in global space BEFORE the function files are compiled?
 
Last edited:
Stick the following in your funcs.h header file:

extern ObjectArray nameofarray;

That tells your compiler that there exists a construct of type ObjectArray somewhere. If you don't define it anywhere, you'll get a linker error later (but you have defined it so you're good).

I think that's what you're after.
 
getting a linker error with that :(

edit: No, missed out a "&" in the "operation(ObjectArray &array);" part of the header file. xyphic you're a genius ad I'll let you have my first son. :D:D:D
 
Last edited:
Back
Top Bottom