- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
好吧,有点上下文,我正在创建一个系统,它可以在文件中添加和删除项目。我首先创建了一个 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();
}
}
这个功能完全没问题。没有错误,如预期的那样,没有警告,什么都没有。
现在跳到我想做同样事情的时候。我希望能够在 addItemForm
类中使用 itemHandler
函数。所以我这样写(在 addItemForm.h
中):
various includes (SDL, iostream, etc)
include "itemHandler.h"
/...........
...represents everything else/
void updateText(int x, int y, SDL_Event e, itemHandler &tmpHander);
现在当我写这个,更具体地说,写 #include "itemHandler.h"
时,编译器 MVSC++2010 不喜欢它。它突然说:error C2061: syntax error : identifier 'addItemForm'
并且无法编译。但是,当我注释掉 itemHandler.h 的包含时,它像正常一样工作。* 我将头文件包含到所有需要使用它们的地方 *
为什么会发生这种情况的唯一想法是与乱用 #include
有关,但我已经尝试解决这个问题,但我不明白这个问题。
感谢任何帮助或见解,谢谢。
最佳答案
您不能让 itemHandler.h
依赖于 addItemForm.h
, 反之亦然。
幸运的是,您不需要!
addItemForm.h
中的代码不需要完整的 itemHandler
定义,只需要知道它是一种类型。那是因为您对它所做的只是声明对它的引用。
所以使用前向声明而不是包含整个 header :
class itemHandler;
事实上,如果你能以相反的方式做同样的事情,那就更好了——保持你的头文件轻量级将减少编译时间,并降低随着你的代码库增加而进一步出现依赖问题的风险。
理想情况下,您只需要真正将每个 header 包含在 .cpp
文件中即可。
关于c++ - 在 .h 文件中使用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34600001/
我尝试在安装了多类型 MFC 库的 visual studio 2015 MFC 上运行以前编写的 MFC c++ 代码。 但是,我这里仍然有 12 个关于缺少函数的错误: IntelliSense:
我正在学习 OOP 并且有疑问。假设我有一个包含 ClassB.h 的文件 ClassA.h,并且在某些时候我的 ClassB.h 需要包含 ClassA .h。 这会产生一个错误,我想我明白为什么会
我开始使用 CUDA 进行编程,在一些示例中我找到了包含文件 cuda.h、cuda_runtime.h 和 cuda_runtime_api.h 包含在代码中。有人可以向我解释一下这些文件之间的区别
我有一些生成正则表达式的代码。那么下面的表达式实际上是: ^(?:\s*((exclude|include|hide|show|protect|risk|dir-merge|merge)),\s*((
我一直在查看一些源代码,以更好地了解我们使用的这款游戏的核心,并编写更可靠、更快速的插件。然后我发现了这段奇怪的代码...... public void setMaxH(double amount)
通常我们会使用标准类型作为 std::unordered_map 的键和值.但现在我需要自定义我自己的键和值类。 键类在block_cache_key.h 中定义如下: #ifndef BLOCK_C
例如,我想要两个头文件,它们可以依赖于另一个头文件中的函数。 //Header1.h file #include Header2.h void h1(){ //... func1(); } v
我正在研究来自 Sedgewick 的 Shell 排序 Algorithms in C part 1-4在第 172 页。 我使用 size (数组的长度),而不是 l和 r (开始和结束);所以我
我在 macOS BigSur 上通过 VMWare 使用 Ubuntu 20.04.2 LTS。我安装了最新版本的 tcl、tcl-dev、tk 和 tk-dev - 版本 8.6。我想编译 Arc
我用我的 glu 和 gl 头文件构建了一个 OpenGL 程序,默认包含在 windows 7 专业版中。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须在我的项目中包含 g
我想在 token 中保留特殊字符,同时仍对特殊字符进行 token 化。说我有话 "H&R Blocks" 我想将其标记为 "H", "R", "H&R", "Blocks" 我读了http://w
关于 hash 作为 trans 参数的另一个问题。在下面的代码中,简单地使用 hash 会给出不正确的结果,但是将其替换为 keys 和 values 会使其正确。怎么了? my @alph1 =
我已经编写了一个 C 程序,它获取屏幕像素的 RGB 值 (0-255),并知道其位置 (x,y)。它可以在 Linux 中运行,但是当我尝试在 Visual Studio (Windows) 中编译
我已经使用 Windows 7 专业版中默认包含的 glu 和 gl 头文件构建了一个 OpenGL 程序。现在,我买了一本描述 OpenGL 游戏开发的书。这本书的作者说,我必须将glew head
#include using namespace std; #include //#include int main() { initscr();
h:messages h:form 内的组件还显示与外部组件相关的消息。 如何限制它只显示与包含 h:form 内的组件相关的消息? 我不喜欢用单独的h:message来使我的代码膨胀。每个输入组件的
我下载了示例代码和 cpp 文件,其中包含 list.h、queue.h 和 vector.h 等头文件,如果我尝试构建,我会收到“ fatal error :没有这样的文件或目录编译终止”我想我应该
我有一个编译成功的桌面项目,但是在我向项目添加新配置以支持 Windows Mobile 平台后,我收到以下错误: error C2146: syntax error : missing ';' be
有很多关于这个错误的帖子,但我无法解决它,我希望你能拿出解决方案。我在 Ubuntu 机器上。 ~/graphmap2$ 在这个文件夹中,我下载了 zlib。可以看图 经过一番谷歌搜索后,我还注意到没
是否可以在 Visual C++ 中使用以下 header : 图.h dos.h bios.h 最佳答案 据我所知,无法在 Visual C++ 中使用它, 与此同时,我希望您关注 Open Wat
我是一名优秀的程序员,十分优秀!