Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在.h文件中使用类_C++_File_Class_Header Files - Fatal编程技术网

C++ 在.h文件中使用类

C++ 在.h文件中使用类,c++,file,class,header-files,C++,File,Class,Header Files,好的,有一点背景知识,我正在创建一个系统,可以从文件中添加和删除项。 我首先创建了一个itemHandler类来处理我拥有的Item类的所有实例。这很有效。 然后,我创建了一个表单来输入值,该表单将用于输入用于创建新项的值,该类称为addItemForm。 所有类都有各自的.h和.cpp文件(例如item/itemHandler/addItemForm.h和.cpp) 我首先在itemHandler类中编写了名为update的函数 void itemHandler::update(int x,

好的,有一点背景知识,我正在创建一个系统,可以从文件中添加和删除项。 我首先创建了一个
itemHandler
类来处理我拥有的
Item
类的所有实例。这很有效。 然后,我创建了一个表单来输入值,该表单将用于输入用于创建新项的值,该类称为
addItemForm
。 所有类都有各自的.h和.cpp文件(例如
item/itemHandler/addItemForm.h
.cpp

我首先在
itemHandler
类中编写了名为
update
的函数

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();
    }


}
现在,当我写这篇文章,更具体地说,写
#include“itemHandler.h”
时,编译器MVSC++2010不喜欢它。它突然说:
error C2061:syntax error:identifier'addItemForm'
,并且没有编译。但是,当我注释掉itemHandler.h的include时,它的工作方式与正常情况类似。 *我将头文件包括到所有需要使用它们的地方*

之所以会发生这种情况,唯一的办法是乱七八糟地使用
#include
,但我已经尝试过解决这个问题,我不理解这个问题

感谢您的帮助或见解,
谢谢。

您不能使用
itemHandler.h
依赖于
addItemForm.h
,反之亦然

幸运的是,你不需要这样做

addItemForm.h
中的代码不需要完整的
itemHandler
定义,只需要知道它是一种类型。这是因为你所做的一切就是声明对它的引用

因此,请使用转发声明,而不是包含整个标头:

 class itemHandler;
事实上,如果你可以用另一种方法做同样的事情,那么保持你的头的轻量级将会减少编译时间,并且随着你的代码库的增加,减少进一步依赖性问题的风险


理想情况下,您只需要在
.cpp
文件中包含每个标题。

您的问题中有
包含“itemHandler.h”
,但它应该是
#包含“itemHandler.h”
。你的问题还是密码有误?红色警报!所有人都到环形附属站去!NathanOliver,这是一个打字错误,是的,我确实写了
#include“itemHandler.h”
只是一个简单的问题,我如何使用前向声明方法来做这样的事情:
void addItemForm::update(itemHandler&tmpHandler)
因此,当包含头文件时,可以将
itemHandler&instance
作为参数传递,并调用函数instance.function()。执行正向声明时(
classitemHander;
)。我还没有弄清楚如何从这个类中调用函数。@Nimrod:对不起,下面没有。如果它没有编译,也许可以试一试,发布一个带有代码错误的新问题。@LightnessRacesinOrbit LOL每次确实需要一些时间来更新你的答案:p
 class itemHandler;