高分求解!!!!!!!!
1。那位兄台能详细的解释一下vc中mfc自动生成的头文件中#if!defined和#define后
面的那一串东西的具体含义。
2。本人在写程序时将好几个源文件都要用到的变量定义成全局变量,并将这些全局变量放置在一头文件中,然后分别include到各个需要用到他们的源文件中,但是vc编译时提示这些变量已经定义过,或是已经出现在某一obj文件中,我按照msdn的指示将编译参数改为/force后,编译通过但是error全变为waring .
请问这是为何?解决办法?
问题点数:100、回复次数:5Top
1 楼weixiao(:D)回复于 2002-03-24 18:14:36 得分 0
加全局变量不能这样加的,也不安全,可以加在应用程序类中,也就是从CWinApp派生的类,在其它类中使用时,只需用应用程序类的指针就可:)Top
2 楼starsoft007(星软)回复于 2002-03-24 18:55:06 得分 0
#include 其实就是把你的代码原封不动的粘贴到你的程序中.你在多个程序中包含了该头文件,所以,就造成重复声明了.
只需要在一个文件中包含该头文件就可以了
至于头文件前面的那些预编译命令,就是说如果前面定义了该头文件,那么此处就不再重复定义了,如果前面没有定义,那么在这里定义Top
3 楼arthor(龙)回复于 2002-03-24 20:53:32 得分 0
#if!defined和#define是宏命令,与if控制差不多
#if!define是如果不定义,#define是定义。
例如
#if!define(#ifndefine) _AFX_H_
#define _AFX_H_
//some code ...
#endif
这么做可以保证一个头文件不被多次的包含.
变量的声明和定义最好不要在头文件中,因为头文件被包含在多个文件中,会导致变量被多次定义,好的编译器应该给出提示。最好把他们定义在应用文件中.cppTop
4 楼prototype(原型)回复于 2002-03-25 08:39:45 得分 100
regarding global variables, as others pointed out that they are not good, you should use them as less as possible. if you do think you have to use global variables, you can do like this:
// put all global variables into a '.cpp' file, for example: 'my_global_variables.cpp'
int my_global_int1;
int my_global_int2;
float my_global_float1;
...
// then write a header file ('my_global_variables.h') like this:
extern int my_global_int1;
extern int my_global_int2;
extern float my_global_float1;
...
you include 'my_global_variables.h' in every file that needs the global variables. when you compile the code, remember to compile and link the 'my_global_variable.cpp' with other files.
Top
5 楼Onre1(完了)回复于 2002-03-25 10:36:10 得分 0
prototype说到点子上了。Top




