Reputation: 43
Ok, a bit of context, I'm creating a system which adds and removes items from a file.
I first created an itemHandler
class to handle all the instances of a Item
class I had. This worked fine.
I then created a form to enter values which would be used to enter values for creating a new item, the class was called addItemForm
.
All classes had their respective .h and .cpp files (e.g. item/itemHandler/addItemForm.h
&.cpp
).
I first wrote this function called update
in the itemHandler
class.
void itemHandler::update(int x, int y, SDL_Event e, addItemForm &tmpForm)
{
if( tmpForm.isViewable == false)
{
if(exportButton.mouseAction(x, y, e))
{
//This funtion is pretty self-explanatory. It exports the item's quantity, name and unit to the inventory file in the format: ITEMNAME[ITEMQUANTITY]ITEMUNIT;
exportItemsToFile();
}
if(reloadButton.mouseAction(x, y, e))
{
//This reloads the item to the vector list 'itemList'. This is done in order to delete items as the list must be refreshed.
reloadItems();
}
for(int i = 0; i < itemNumber; i++)
{
//This FOR loop runs the update function for each item. This checks if any action is being preformed on the item, e.g. changing the item quantity, deleting the item etc
itemList.at(i).update( x, y, e );
}
//The 'checking' for if the user wants to delete an item is done within the function 'deleteItem()'. This is why there is not IF or CASE statement checking if the user has requested so.
deleteItem();
}
}
This function worked completely fine. No errors, as intended, no warnings, nothing.
Now skip to when I want to do the same sort of thing. I want to be able to use an itemHandler
function in the addItemForm
class. So I write this (in addItemForm.h
):
various includes (SDL, iostream, etc)
include "itemHandler.h"
/...........
...represents everything else/
void updateText(int x, int y, SDL_Event e, itemHandler &tmpHander);
Now when I write this, and more specifically, write #include "itemHandler.h"
, the compiler MVSC++2010 doesn't like it. It suddenly says: error C2061: syntax error : identifier 'addItemForm'
and doesn't compile. However, when I comment out the include of itemHandler.h it works like normal.
* I included the header files to all places which need to use them *
The only idea for why this might be happening is to do with using #include
messily but I've tried clearing this up and I don't understand the problem.
Any help or insight is appreciated, Thanks.
Upvotes: 2
Views: 283
Reputation: 385144
You can't have itemHandler.h
depend on addItemForm.h
and vice versa.
Fortunately, you don't need to!
That code in addItemForm.h
doesn't require the full itemHandler
definition, just the knowledge that it's a type. That's because all you're doing with it is declaring a reference to it.
So use a forward declaration instead of including the whole header:
class itemHandler;
In fact, if you can do the same thing the other way around then so much the better — keeping your headers lightweight will decrease compilation times and reduce the risk of further dependency problems down the line as your codebase increases.
Ideally, you only need to really include each header inside the .cpp
files.
Upvotes: 3