As my project for a c++ course, I'm making a sort of small console game. I am trying to call a function from class Player in Player.h (shown below) in class Itembase contained in Items.h (also below). Items.h does not seem to be recognizing the class Player from the other file and I'm not sure why. In another file in this same project I call another function from class Player and it works just fine. I'm not sure why Items.h doesn't seem to be recognizing the include Player.h.
I am getting these errors.
Error 2 error C2228: left of '.editwepatk' must have class/struct/union 31
Error 6 error C2228: left of '.editspell' must have class/struct/union 39
Error 4 error C2228: left of '.editarmor' must have class/struct/union 35
Error 1 error C2065: 'player' : undeclared identifier 31
Error 3 error C2065: 'player' : undeclared identifier 35
Error 5 error C2065: 'player' : undeclared identifier 39
all in the Items.h file.
The Player.h file uses several files not include here. They are the classes inheriting class Itembase, but don't seem to be any part of this particular problem.
Ya, I started doing the project a couple weeks ago when I just started learning classes and I didn't realize that was the proper way of doing it. It's due in about a week and I haven't had a chance to change that around.
I would recommend that you do split the implementation out of the header files as soon as possible, then fix the problems that arise. You never know, some problems may in fact disappear ;-).
(also, a hint for the declaration of the player global variable - in Player.h, define the class first "class Player { ... };" then declare the global instance of it "extern Player player;", then make sure to define it in the Player.cpp "Player player;" otherwise you'll end up with a linker error).
Ok, I split the two files including the header files within the .cpp files and put "extern Player player;" within the header then defined "Player player;" in the cpp. I am getting something like 52 errors along the lines of
"Error 34 error LNK2005: "public: void __thiscall Player::weapons(void)" (?weapons@Player@@QAEXXZ) already defined in Items.obj"
and I'm notexactly sure what that means.
That means that there is still a duplication of code in the files -- somewhere in Items.cpp (which is what is compiled to make Items.obj) there is an instance of "void Player::weapons(void) { ... }" (perhaps coming from a header file that still has code in it).
Some things to check:
(a) I hope you're not #include'ing the .cpp files anywhere? only header files should be #include'd, because they describe structures and class declarations to be used in the logic;
(b) I hope you've removed the "void Player::weapons(void) { ... }" code from the .h file?
(c) I hope you've done a clean recompile? (ie delete all .obj's before rebuilding, in case you have dependency issues in your makefile/project)
(d) if all of the above checks out okay, then I would suggest either getting your compiler to spit out the result of the preprocessing pass ("g++ -E" if you're using GCC, not sure what the equivalent is for MSVC) and read it through until you find the duplication, alternatively track through the #include's yourself until you find where the duplication is coming from, and remove it.
Thank you so much for all the help. I had accidentily included a the cpp where I thought I had included a header. The program compiles and functions the way I need it to. This was the only major snag I ran into that me and the lab aides could not figure out. Thank you again.