免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3574 | 回复: 5

[C++] 头文件grobal_var.h被两个cpp包含就产生错误? [复制链接]

论坛徽章:
0
发表于 2014-06-16 17:29 |显示全部楼层
本帖最后由 xuebabybaby 于 2014-06-16 19:27 编辑

以下代码看以来有点多, 大概是这样的, 在我的头文件grobal_var.h中使用了
  1. //grobal_var.h
  2. #ifndef GROBAL_VAR_H_
  3. #define GROBAL_VAR_H_
  4. ...
  5. typedef INT32 st_ClientMapKey;  //在std::map<>中使用
  6. typedef struct
  7. {
  8.         UCHAR ucChannel;
  9.         st_ETIM_Session SessionInfo;
  10.         UINT32 uConnectTime; //connect before ? seconds or mini seconds
  11.         CHAR bUsed;
  12. }st_ClientMapValue;
  13. typedef std::pair<st_ClientMapKey, st_ClientMapValue> PairClient;  
  14. class SingletonMapClient
  15. {
  16. public:
  17.         .........
  18.         std::map<st_ClientMapKey,st_ClientMapValue> m_mapClient;  // pair<sessionhanle, st_clientMapValue>   //map
  19. ........
  20. public:
  21.         ..........
  22. private:
  23.         .......
  24. };

  25. #endif  //GROBAL_VAR_H_
复制代码
grobal_var.cpp文件

  1. //LoginPack.cpp文件
  2. #inlclude "grobal_var.h
  3. //其它 实现
复制代码
而在我的另一个源文件中只要引用了grobal_var.h就出错, 这是为什么呢? 谢谢
  1. //LoginPack.cpp
  2. #include "ETIM/E_P2P_API.h"
  3. #include "AVSTREAM_IO_Proto.h"
  4. #include "LoginPack.h"
  5. #include "implement/grobal_var.h"
  6. [color=Red]//加上include "implement/grobal_var.h" 会产生这样的错误:
  7. /*1>  LoginPack.cpp
  8. 1>c:\program files\microsoft visual studio 10.0\vc\include\limits(378): error C2766: 显式专用化;已定义“std::numeric_limits<char>”
  9.         1>          c:\program files\microsoft visual studio 10.0\vc\include\limits(187) : 参见“numeric_limits<char>”的前一个定义
  10.         1>c:\program files\microsoft visual studio 10.0\vc\include\xtr1common(244): error C2766: 显式专用化;已定义“std::tr1::_Is_integral<CHAR>”
  11.         1>          c:\program files\microsoft visual studio 10.0\vc\include\xtr1common(236) : 参见“_Is_integral<char>”的前一个定义
  12.         1>c:\program files\microsoft visual studio 10.0\vc\include\type_traits(1092): error C2766: 显式专用化;已定义“std::_Arithmetic_traits<CHAR>”
  13.         1>          c:\program files\microsoft visual studio 10.0\vc\include\type_traits(1084) : 参见“_Arithmetic_traits<char>”的前一个定义
  14.         1>c:\program files\microsoft visual studio 10.0\vc\include\xutility(411): error C2766: 显式专用化;已定义“std::iterator_traits<std::_Bool>”
  15.         1>          c:\program files\microsoft visual studio 10.0\vc\include\xutility(404) : 参见“iterator_traits<char>”的前一个定义*/
  16. #include "implement/grobal_function.h"[/color]
  17. AbsLoginPack::AbsLoginPack()
  18. {
  19.      //其它实现
  20. }
复制代码

论坛徽章:
0
发表于 2014-06-16 17:47 |显示全部楼层
而且 #include "ETIM/E_P2P_API.h"
#include "AVSTREAM_IO_Proto.h"
#include "LoginPack.h"
这三个文件中都没用#include"grobal_var.h"

论坛徽章:
0
发表于 2014-06-16 20:08 |显示全部楼层
下面就贴一下grobal_var.h:
  1. #ifndef GROBAL_VAR_H_
  2. #define GROBAL_VAR_H_

  3. #ifdef WIN32
  4. #include<stdio.h>
  5. #include<map>
  6. #endif
  7. #ifdef LINUX
  8. #include<stdio.h>
  9. #include<map>
  10. #endif

  11. #include "ETIM/ETIM_Type.h"
  12. //#include "ETIM/E_P2P_API.h"
  13. #include "ETIM/E_P2P_API.h"
  14. #ifdef WIN32
  15. #include<windows.h>               
  16. #endif


  17. #if defined(WIN32DLL) || defined (WIN32)
  18. #define mSecSleep(ms)        Sleep(ms)
  19. typedef         DWORD                        ETIM_threadid;
  20. #elif defined(LINUX)
  21. #define mSecSleep(ms)        usleep(ms * 1000)
  22. typedef pthread_t                ETIM_threadid;
  23. #endif

  24. #ifdef WIN32
  25. class Locker
  26. {
  27. public:
  28.         inline Locker() { m_hMutex=CreateMutex(NULL,FALSE,NULL); }
  29.         inline ~Locker() { CloseHandle(m_hMutex); }
  30.         inline void Lock() { WaitForSingleObject(m_hMutex, INFINITE); }
  31.         inline void Unlock() { ReleaseMutex(m_hMutex); }
  32. private:
  33.         HANDLE m_hMutex;
  34. };

  35. #endif
  36. #ifdef LINUX
  37. #endif

  38. extern Locker g_lockLock;



  39. /*class SingletonMapClient*/
  40. typedef INT32 st_ClientMapKey;
  41. typedef struct
  42. {
  43.         UCHAR ucChannel;
  44.         st_ETIM_Session SessionInfo;
  45.         UINT32 uConnectTime; //connect before ? seconds or mini seconds
  46.         CHAR bUsed;
  47. }st_ClientMapValue;
  48. typedef std::pair<st_ClientMapKey, st_ClientMapValue> PairClient;
  49. //typedef std::map<st_ClientMapKey,st_ClientMapValue> ClientValueType;

  50. class SingletonMapClient
  51. {
  52. public:
  53.         SingletonMapClient();
  54.         virtual ~SingletonMapClient();
  55.        
  56.         std::map<st_ClientMapKey,st_ClientMapValue> m_mapClient;  // pair<sessionhanle, st_clientMapValue>

  57. public:
  58.        

  59.         static SingletonMapClient * GetInstance();
  60. private:
  61.         static SingletonMapClient * m_s_pInstance;
  62.         static Locker m_s_lockSingle;
  63. };


  64. /*class SingletonReceiveCmd*/
  65. typedef INT32 st_ReceiveMapKey;
  66. typedef struct
  67. {
  68.         UCHAR ucChannel;
  69.         char szFilePathName[MAX_PATH];
  70.         long lOffset;
  71.         size_t uSize;
  72.         ETIM_threadid dwThreadId;
  73.         int iSending;

  74. }st_ReciveMapValue;
  75. typedef std::pair<const st_ReceiveMapKey, st_ReciveMapValue> PairReceiveCmd; //pair<sessionHandle, ReceiveMapValue>
  76. //typedef std::multimap<st_ReceiveMapIndex, st_ReciveMapValue> RecevieCmd_ValueType;
  77. class SingletonReceiveCmd
  78. {
  79. public:
  80.         SingletonReceiveCmd();
  81.         virtual ~SingletonReceiveCmd();
  82.         std::multimap<st_ReceiveMapKey, st_ReciveMapValue> m_multimapReceiveCmd;
  83. public:
  84.         static SingletonReceiveCmd * GetInstance();
  85. private:
  86.         static SingletonReceiveCmd * m_s_pInstance;
  87.         static Locker m_s_lockSingle;

  88. };



  89. /*a Facade Pattern for all Grobal variable*/
  90. class GrobalVarFacade
  91. {
  92. //GrobalVarFacade 中的每个函数都要对互斥量加锁和解锁
  93. public:
  94.         GrobalVarFacade();
  95.         virtual ~GrobalVarFacade();
  96. public:
  97.         SingletonMapClient * GetSingeltonMapClientInstance();
  98.         SingletonReceiveCmd * GetSingletonReceiveCmdInstance();
  99.         bool AddClient(PairClient pairCli);
  100.        
  101.         bool FindClient(st_ClientMapKey cliIndex);
  102.         bool DeleteClient(st_ClientMapKey cliIndex);
  103.         UINT32 GetClientCount();

  104.         bool GetOneReceiveCmd(st_ReceiveMapKey &key ,st_ReciveMapValue &Value);

  105.         bool AddOrUpdateClient();
  106.        
  107. private:
  108.         static GrobalVarFacade *m_s_pInstance;
  109.         static Locker m_s_lockGrobalVar;

  110.         SingletonMapClient * m_p_SingletonClient;
  111.         SingletonReceiveCmd * m_p_SingetonReceiveCmd;
  112. };

  113. #endif //#ifndef GROBAL_VAR_H_
复制代码

论坛徽章:
0
发表于 2014-06-16 21:46 |显示全部楼层
代码比较长, 大体上是这样的:
SingletonMapClient-----类里面有一个 std::map<st_ClientMapKey,st_ClientMapValue> m_mapClient, 存放的是有序对<会话句柄, 客户端信息> .  为了保证只创建一个实例才封装成类 SingletonMapClient

SingletonReceiveCmd----类里面有一个std::multimap<st_ReceiveMapKey, st_ReciveMapValue> m_multimapReceiveCmd, 存放有是有序对<会话句柄, 要请求的命令>

GrobalVarFacade ---类是在多线程序中保证互斥访问上面两个类唯一的全局变量....

LoginPack.cpp 想调用这几个类, 可是在LoginPack.cpp一一包含头文件grobal_var.h: #include "grobal_var.h"就编译不通过阿......
提示的信息就像一楼的那样????
说是"已定义“std::numeric_limits<char>"
"已定义:std::tr1::_Is_integral<CHAR>" 之类的

论坛徽章:
0
发表于 2014-06-16 22:04 |显示全部楼层
PS: 我有用到iterator,  有百度了一下: 好像说是 与iterator_traits 有关? 是要对iterator进行重新定义吗?

小弟初学,不是很懂,求教

论坛徽章:
0
发表于 2014-06-18 08:47 |显示全部楼层
原来 是使用STL标准头文件出的问题.......

我在grobal_var.h中将#include<map>删除, 所有有到grobal_var.h的.cpp文件中都这样 使用include:
#include<map>
#include"grobal_var.h"
这样就解决了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP