mfc中,
自己声明了一些enum和struct结构,打算在程序中的几个文件中用,本来我的做法是:在这些enum和struct结构写在一个独立的a.h,a.cpp文件中,然后在每个要用到它的地方包含该头文件,但是报错说那些在a文件中的类型在其它文件中不能识别,于是将enum和struct结构重新写在每个用到的文件的class声明之外,报错说重定义。最后干脆不用包含a文件,直接将enum和struct结构写在每个用到的class的class声明之内,没有错误。难道就没办法将程序中普遍用到的数据类型写入一个头文件,然后其它文件包含它就行的方法吗????

解决方案 »

  1.   

    可以的在这些enum和struct结构写在一个独立的a.h,a.cpp文件中???
    还需要放在a.cpp中???
      

  2.   

    不需要,a.cpp中只是#include"stdafx.h"和#include"a.h",但是照我最后没错误的那样做,不同class的相同名称的struct,enum不能互相赋值,因为是在不同class声明,作用域不同,这样只能用一个头文件包含了。可以的话怎么做????
      

  3.   

    //GeneralDef.henum RecordState
    {
    State_Invalid,
    State_Ready,
    State_Recording,
    };typedef struct tagIdNameDescription{
    int id; ///< 错误号
    const char * name; ///<错误名 
    const char * description; ///< 错误描述
    }IdNameDescription;
      

  4.   

    声明在stdafx.h应该没问题吧,我以前这样做过啊
      

  5.   

    to ouyh12345 :头文件里只声明枚举和结构 
    是否还定义了变量?/////////////////////////
    我是写成这种形式的,只声明这种类型//a.h中
    typedef enum{A,B,C}num;
    typedef struct
    {
     num n1;
    }data;
    //然后在其它文件中定义data型变量//其它用到的文件中,例如file1.cpp中
    #include"a.h"class file1
    {
        public:
        data d1;//错误,没定义
    ......
    };   
      

  6.   

    自己写了个简单的//test.h
    #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里的内容
    ......
    、、、、、、、、、、、、、、、、
      

  7.   

    把结构体、枚举类型定义都声明在一个a.h中,然后在stdafx.h文件做一下包含,这样在整个工程的任何地方都可以引用了。
      

  8.   

    首先你在头文件中的类声明用到了data这个struct,所以是要包含test.h的
    如果没有在头文件中包含就会提示未定义
      

  9.   

    你们说得都没错但我现在问题是:///////////////////////////////////////////////////////// 这样能够编译连接通过。 
    请注意这里的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里的内容 
    ...... 
    、、、、、、、、、、、、、、、、
      

  10.   

    //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都能编译通过的,是不是有其它问题?
      

  11.   


    //a.cpp
    #include "a.h"
    ...//a.h
    #include "common.h"
    ...//common.h
    ...