混淆.cpp和.h [英] confuse about .cpp and .h

查看:187
本文介绍了混淆.cpp和.h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况:

我创建一个包含2个表单的项目:form1和form2
并因此生成form1.h form1.cpp form2.h form2.cpp.

当我在form1.h中声明函数int addnumber(int ,int )
并在form1.cpp中,我将执行以下操作:

my situation:

i create a project contains 2 forms:form1 and form2
and so it generate form1.h form1.cpp form2.h form2.cpp.

when i declare a function int addnumber(int ,int ) in form1.h
and in the form1.cpp i will do this:

#include<form1.h>
int addnumber(int a,int b){
   a+=b
   return a}



在form2中,当我需要使用addnumber函数时,我需要包括form1.h或.cpp ??
如果我包含form1.h,那么addnumber将没有任何功能权(因为该功能在form1.cpp中)?

另一个问题是form1控件(按钮,标签等)的事件和方法会自动存储在form1.h中?因此,当我需要使用form2引发和form1控件的事件时,我只需要包含form1.h ??
如果是这样,当我必须使用form1引发form2控件的事件时,该怎么办?
如果我包含form2.h,我将得到不确定的循环,因为form1.h包含form2.h,form2.h包含back form1.h?



while in form2 when i need to use the addnumber function,i need to include form1.h or .cpp??
if i include form1.h the addnumber wont have any function right(as the functioning is in form1.cpp)?

Another question is the form1 controls(buttons ,labels..etc)events and methods is auto store in form1.h?so when i need to use form2 to raise and event of form1''s control i just have to include form1.h??
If it is so when i have to use form1 to raise event of form2''s control how can i do that?
if i include form2.h ,will i get indefinite loop as form1.h include form2.h and form2.h include back form1.h?

推荐答案

.h文件(通常)定义与某物的接口.它们包含您使用特定抽象所需的最少信息量-是类,函数还是变量(如果您很生气).

.cpp文件(通常)包含某些内容的实现.它们包含用户使用抽象不需要知道的内容.

因此,如果您具有添加功能,则可以在.h文件中放入使用它所需的最少信息:

.h files (generally) define the interface to something. They contain the minimum amount of information you need to use a particular abstraction - be it a class, function or variable (if you''re mad).

.cpp files (generally) contain the implementation of something. They contain the stuff that users don''t need to know to use the abstraction.

So if you have an add function, you put the minimum amount of information you need to use it in the .h file:

// add.h

#ifndef ADD_H_
#define ADD_H_

int add( int a, int b );

#endif



然后任何想要使用该功能的人都可以包含.h文件:



Then anyone that wants to use the function can just include the .h file:

#include <iostream>
#include "add.h"

int main()
{
    std::cout << add( 1, 3 ) << std::endl;
}



这将使编译器保持满意状态,并且将编译其中包含main的文件.它不会链接.要建立代码链接并运行,您必须提供add函数的实现:



This will keep the compiler happy and the file with main in it will compile. It won''t link though. To make the code link and run you have to provide an implementation of the add function:

#include "add.h"

int add( int a, int b )
{
    return a + b;
}



所以基本上的规则是:

-您要使用添加功能的每个文件中都包含add.h
-在每个程序中,您都使用add函数进行编译并链接add.cpp

当您想使用库时,还有一些其他功能,但希望对您有所帮助.

第二部分...如果最终遇到循环包含依赖关系,那么您的设计就有问题.同时在屏幕上显示两个而不是层次结构的对话框通常不会很好地工作.如果您确实必须相互包含,那么我在顶部列出的.h文件的格式会有所帮助,但我建议您阅读Herb Sutter撰写的"Exceptional C ++",其中有非常好的两节内容如何设计和实现类.

干杯,



PS:编辑因为我今天早上英语不方便



So basically the rules are:

- in every file you want to use the add function include add.h
- in every program you use the add function compile and link add.cpp

There are some extras when you want to use libraries but hopefully this''ll help.

For the second part... If you end up with cyclic include dependencies then then there''s something wrong with your design. Having two, not hierarchal, dialogue boxes up on the screen at the same time doesn''t tend to work very well. If you really must have mutual inclusion then the format for the .h file I''ve laid out at the top will help but I''d advise you to read "Exceptional C++" by Herb Sutter which has an excellent couple of sections on how to design and implement classes.

Cheers,

Ash

PS: Edited ''cause I''m having trouble with my English this morning


如果在form2.cpp文件中包含form1.h,则编译器将看到函数的定义并能够基于该定义生成对外部函数的正确函数调用.当两个对象模块被链接以创建可执行对象时,链接器将满足该调用.

也许您需要花一些时间来了解C/C ++程序的基本概念,以便更好地理解.
If you include form1.h in your form2.cpp file then the compiler will see the definition of your function and will be able to generate a correct funtion call to the external function based on that definition. The linker will then satisfy the call when the two object modules are linked to create the executable object.

Perhaps you need to spend some time on the basic concepts of a C/C++ program to get a better understanding.


您的基本问题是您没有获得总体概念.如果您需要同时使用两种形式的方法,请将其放入其自己的类中,并将其包括在两种形式中.
Your basic issue is that you don''t get the overall concept. If you need to use a method in both forms, put it in it''s own class and include it in both.


这篇关于混淆.cpp和.h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆