mfc中,
自己声明了一些enum和struct结构,打算在程序中的几个文件中用,本来我的做法是:在这些enum和struct结构写在一个独立的a.h,a.cpp文件中,然后在每个要用到它的地方包含该头文件,但是报错说那些在a文件中的类型在其它文件中不能识别,于是将enum和struct结构重新写在每个用到的文件的class声明之外,报错说重定义。最后干脆不用包含a文件,直接将enum和struct结构写在每个用到的class的class声明之内,没有错误。难道就没办法将程序中普遍用到的数据类型写入一个头文件,然后其它文件包含它就行的方法吗????
自己声明了一些enum和struct结构,打算在程序中的几个文件中用,本来我的做法是:在这些enum和struct结构写在一个独立的a.h,a.cpp文件中,然后在每个要用到它的地方包含该头文件,但是报错说那些在a文件中的类型在其它文件中不能识别,于是将enum和struct结构重新写在每个用到的文件的class声明之外,报错说重定义。最后干脆不用包含a文件,直接将enum和struct结构写在每个用到的class的class声明之内,没有错误。难道就没办法将程序中普遍用到的数据类型写入一个头文件,然后其它文件包含它就行的方法吗????
还需要放在a.cpp中???
{
State_Invalid,
State_Ready,
State_Recording,
};typedef struct tagIdNameDescription{
int id; ///< 错误号
const char * name; ///<错误名
const char * description; ///< 错误描述
}IdNameDescription;
是否还定义了变量?/////////////////////////
我是写成这种形式的,只声明这种类型//a.h中
typedef enum{A,B,C}num;
typedef struct
{
num n1;
}data;
//然后在其它文件中定义data型变量//其它用到的文件中,例如file1.cpp中
#include"a.h"class file1
{
public:
data d1;//错误,没定义
......
};
#ifndef TEST_H
#define TEST_H
#pragma onceenum num{A,B,C};typedef struct
{
num n1;
}data;#endif
、、、、、、、、、、、、、、、、、、、、、、、、、、、、
//test.cpp
#include "stdafx.h"
#include "test.h"
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
// gameView.cpp
#include "test.h"
#include "gameView.h"
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
// gameView.h
.....
class CGameView : public CFormView
{
.......
data d1;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
// gameDoc.h
#include "test.h"
.....
class CGameDoc : public CDocument
{
.....
data d2;
}
、、、、、、、、、、、、、、、、、、、
//gameDoc.cpp
//不作改动
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
// dlg1 dialog
// dlg1.h
class dlg1 : public CDialog
{
......
data d3;
}
、、、、、、、、、、、、、、、、、、、、、、
// dlg1.cpp #include "test.h"#include "dlg1.h"
/////////////////////////////////////////////////////////这样能够编译连接通过。
请注意这里的gameDoc.h,
#include "test.h"只能放在这里,而不能放在gameDoc.cpp文件,否则出错,说d2未定义。还发现#include "test.h"加在gameView.h,dlg1.h的类声明前也没错。是否可的出结论:
只要把#include "test.h"加在将要使用文件的.h肯定编译连接通过呢?如果是这样,我就不明白了,对于gameDoc,#include "test.h"为什么只能放在.h文件,更一般情况,例如:
//////////
情况一:
//a.cpp
#include "test.h"
#include "a.h"
//a.h
.......//////
情况二:
//a.cpp
#include "a.h"
//a.h
#include "test.h"
.......以上两种情况不是一样的吗?
a.cpp都可以转化为
//a.cpptest.h里的内容
a.h里的内容
......
、、、、、、、、、、、、、、、、
如果没有在头文件中包含就会提示未定义
请注意这里的gameDoc.h,
#include "test.h"只能放在这里,而不能放在gameDoc.cpp文件,否则出错,说d2未定义。还发现#include "test.h"加在gameView.h,dlg1.h的类声明前也没错。 是否可的出结论:
只要把#include "test.h"加在将要使用文件的.h肯定编译连接通过呢? 如果是这样,我就不明白了,对于gameDoc,#include "test.h"为什么只能放在.h文件, 更一般情况,例如:
//////////
情况一:
//a.cpp
#include "test.h"
#include "a.h"
//a.h
....... //////
情况二:
//a.cpp
#include "a.h"
//a.h
#include "test.h"
....... 以上两种情况不是一样的吗?
a.cpp都可以转化为
//a.cpp test.h里的内容
a.h里的内容
......
、、、、、、、、、、、、、、、、
typedef enum{A,B,C}num;
typedef struct
{
num n1;
}data;
//然后在其它文件中定义data型变量 //其它用到的文件中,例如file1.cpp中
#include"a.h" class file1
{
public:
data d1;//错误,没定义
......
};
=================================================
我这里试下来vc和.net都能编译通过的,是不是有其它问题?
//a.cpp
#include "a.h"
...//a.h
#include "common.h"
...//common.h
...