头文件重引用的问题

xinqiao2008 2010-07-16 01:14:41
编译Debug正常,但编译Release时出错

msvcprt.lib(MSVCP71.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in P2Ppublic.lib(B2.obj)

//主程序
WinMain()
#include <A1.h>
//...//

A1.h
#ifndef A1
#define A1
#include <B2.h>
#include <string>
using namespace std;
//...//
#endif

B2.h
#ifndef B2
#define B2
#include <A1.h>
#include <string>
using namespace std;
//...//
#endif


#include <string>
using namespace std;
引起的,但不知如何解决?
...全文
123 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuxiabo1984 2010-07-16
  • 打赏
  • 举报
回复
A1.h 和A2.h相互包含
zhh_kv 2010-07-16
  • 打赏
  • 举报
回复
#endif
zhh_kv 2010-07-16
  • 打赏
  • 举报
回复
建议在头文件中:添加
#ifndef ...
#define ...

#enddef
xt0601 2010-07-16
  • 打赏
  • 举报
回复
一楼正解,顶一楼!
Lonelywolf1899 2010-07-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]

你这个A1.h和B2.h怎么相互包含呢
[/Quote]

一针见血

xinqiao2008 2010-07-16
  • 打赏
  • 举报
回复
string不能移到cpp里面,因这个头文件其它文件也用到。
kim_jinye 2010-07-16
  • 打赏
  • 举报
回复
把string挪到.cpp中,或A引用了string,B就不需要再引用了,因为B已经引用了A,所以B其实也引用了string。
Simao 2010-07-16
  • 打赏
  • 举报
回复
你可以新建一个头文件M.h,把这些公用的头文件包含进入,然后在B1.h,B2.h中#include "M.h",这样可以了.
pengzhixi 2010-07-16
  • 打赏
  • 举报
回复
你这个A1.h和B2.h怎么相互包含呢

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧