Calling from separate part of program

Hi. I'm working on a very code (but I'm very new to this).
For a specific .cc file I am trying to verify that the program is updating specific variables by inserting a cout << and cin.get();
The information I want to display is being calculated in a different part of the program which is in an entirely different directory. For example, I am writing the code in heatconduction/hc.cc and the calculated variables are found in grid/grid.cc.

The format for the variables is something like:
face[f].normal
cell[c].centroid
Where f and c are just the index for a number in a loop.

I need to call these variables from grid/grid.cc in my section of the code but I can't figure out a working syntax.

 
  cout << "Face Normal = "<<grid[gid].face[f].normal<<endl;


This was an attempt I made but when I compile it doesn't recognize the f index identifier.

Additionally I was given a hint that there are other parts of the heat conduction directory that contain the [f] and [c] indices, hinting that I should be able to use that to identify the variables.

Any help is greatly appreciated!
thank you!
JM COOPER
If you want to use code from different files, you need to #include the file where that code exists.
@ OP: You accomplish what ResidentBiscuit is talking about by including the headers where the variables are declared. This is functionally the same as making any other forward declaration. The compiler will know that the definition of what ever variable is going to be made some where else, and it will know to look for it later. As for the directories, neither C or C++ are interpreted languages so after you compile the binary your source code and the files that contain it cease to matter.
Last edited on
Let me explain further:

Both files are in a larger directory, src_3s.
In grid/grid.cc, it calls a function void grid::areas_volumes.
This function does the calculations that generate
face[f].normal
cell[c].centroid
etc
I have written a function in heat_conduction/hc.cc that needs to access these variables after they have been calculated to show that they have been updated.
So you are saying in hc.cc I should put a header like:

#include grid/grid.cc

And then I can call the variables as they are written in the grid.cc file?

Also, heat_conduction and grid are both in src_3s directory.

Thanks
JM COOPER
Please do explain further, start with why it is you think that the directory structure of your operating system matters at all the the vtable after compilation of your code. The only thing I can think of is that you want us to confirm that you are designating the directory correctly but the only way for us to do that without a margin of error is to either use Team Viewer to connect to your machine or to tell you to use literal paths.

The extension .cc suggests that you are referring to source code files. Those get linked separately, they do not get included into any other files. What the macro #include does is tell the linker to copy and paste the contents of the header file you designate into that point of the source code file it is written in just before creating the object file. This is why source code files have corresponding object files and headers do not. Compiling the object files is just a matter of telling your IDE to add the source code files to your project. The issue you are having is with scope, specifically you need to forward declare the data you are trying to use in a header file so that your compiler knows to look for the definition of that data elsewhere.

AFTER THOUGHT: I don't know if you just mis-typed or if you omitted them for a reason, but your #include statement is missing a set of brackets. You use angular brackets "<>" for files that are in your IDE's search path and you use double quotes to refer to directories that are relative to your working directory. I don't mean to be condescending here, I obviously don't know who you are in meatspace and I have no idea what your experience level is so I'm trying to error in the side of caution by providing more information then might be strictly necessary.
Last edited on
Topic archived. No new replies allowed.