Can't access variables in my header file

cplusplus forum,
C++ MS VS 2017 Windows 7

I have one of my header files, that I have used before, #included in my
solution. It compiles ok and the linker/loader doesn't complain. I can load
this header file into the IDE window but I can't access any of the variables
from this file in my .cpp.

Any suggestions?

Jerryd
#included in my solution.
What does that mean? You can either #include a file in a source file, or add a file to a solution. You don't #include a file in a solution, that's a meaningless phrase.

If you merely added the header to the solution, that's a no-op for the toolchain. Header files are neither compiled nor linked. The only point of adding a header to a solution is to be able to edit it more comfortably, and possibly to assist the autocompletion tool. To use the declarations in it you still have to include it in a source file.
helios,
Thanks for replying.

Sorry, I didn't put enough detail in my post. The actual line is:
#include "ScoreChart.h"

immediately under that line is:
#include "dirent.h"

both of these header file reside in the directory with the .cpp file. I have no problem with the
dirent.h file.

To move on with coding I put the entire contents of the ScoreChart.h file at the top of my .cpp.
Everything works that way but I don't want to leave it because it's a large file that doesn't belong there.

Jerryd
You're going to need to show your code, otherwise it will just be a guessing game.

cplusplus forum,

In ScoreChart.h there is a array named int majors[][6];

Here's a short version of my program.

#include "ScoreChart.h"
#include "dirent.h"

int main()
{

int* majorpnt;

majorpnt = &majors[0][0];

return 0;
}

I have used similar code with the same ScoreChart.h header file in other
programs. For some reason this variable and any other variables in
ScoreChart.h" aren't found by the compiler.

This works when I copy the ScoreChart.h into my .cpp file.

Jerryd

To move on with coding I put the entire contents of the ScoreChart.h file at the top of my .cpp.


Keeping it all as separate files is the norm, as you know.

If you have #include'd "ScoreChart.h" in your "ScoreChart.cpp" file, if in fact you have one, then it's probably a path problem. Another possibility is you haven't add'ed the "ScoreChart.cpp" file to the Solution/Project, or whatever the terminology is with VS2017

What's the error message from the compiler?. Also, post the contents of ScoreChart.h
againtry,
Thanks for the reply.

Yes putting all this at the top of my program is a mess.

The platform is Visual Studio 2017 and my .cpp isn't named ScoreChart.cpp.

Hard to think there is a path problem since neither the compiler or linker
linker complain that it can't be found.

Jerryd
cplusplus,
There was no error message from the compiler or linker.

There must have been something corrupted in my project or solution as MS
calls it because I started a whole new one and when I put in my #include it
worked. Then I just cut and pasted all the rest of my code into the new
project. The name of the .cpp is slightly different but that's ok.

Thank,
Jerry
@jerryd

Sounds now that you have 2 files - 1 is ScoreChart.h, and the other which contains main() is called xyz.cpp

I was thinking there were 3 - ScoreChart.h, ScoreChart.cpp and then the third which contains main(), called xyz.cpp.

Either way the files have to be add'ed to the solution.

Overall despite your latest fix, which is the important thing - perhaps next time try a 'clean and rebuild'. Could save a bit of time.
againtry,
I did do a clean and rebuild but it didn't help. I will just delete that entire project and move on.

Jerryd
In ScoreChart.h there is a array named int majors[][6];


In the header that array should be extern qualified, and it should be defined in one and only one source file.

This is like trying to diagnose a problem with your car when you haven't actually brought the car to the shop.

Show all of your source code files.
Topic archived. No new replies allowed.