由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - forward declaration
相关主题
怎样include一个函数请教有关header file的几个问题
ask a question about compile shared library using libtoolWhat is the difference between class and struct?
关于C++ STL编译的疑问C 里面有办法永久改变一个指针的属性吗?
基础问题:在header里面define functiongcc 编译的时候要包括 header source file 吗?
一道面试怪题C++. (转载)问低级问题
typedefclass D:public B;
c++ initialize structC++ memcpy declaration use restrict keyword?
Question about friend in C++弱问c++里有没有NULL这个keyword?
相关话题的讨论汇总
话题: class话题: forward话题: 包含话题: struct
进入Programming版参与讨论
1 (共1页)
S*******s
发帖数: 13043
1
header1.h:
struct A{
int a;
};
header2.h:
class A;
class B{
A* pA;
}
如果某cpp只包括这两个中的一个,都没问题。可是如果需要都包含,比如
combined.cpp:
#include "header1.h"
#include "header2.h"
悲剧了,编译器抱怨重复定义。我现在只好在header2里面加上条件编译,如果A已经定
义了就跳过提前声明。同时包含两个头文件的时候必须先包含header1,后包含header2
。 感觉很不爽。 what's neater solution?
b***i
发帖数: 3043
2
1就这样,2包含1不就行了?把那个class A;去掉

【在 S*******s 的大作中提到】
: header1.h:
: struct A{
: int a;
: };
: header2.h:
: class A;
: class B{
: A* pA;
: }
: 如果某cpp只包括这两个中的一个,都没问题。可是如果需要都包含,比如

f*******n
发帖数: 12623
3
There is no problem here. The exact code you showed compiles fine (except for the missing semicolon after class B). Your problem must be somewhere else.

【在 S*******s 的大作中提到】
: header1.h:
: struct A{
: int a;
: };
: header2.h:
: class A;
: class B{
: A* pA;
: }
: 如果某cpp只包括这两个中的一个,都没问题。可是如果需要都包含,比如

X****r
发帖数: 3557
4
You have to use the same keyword 'struct' or 'class' when
declaring an incomplete class type. Change 'class A;' in
header2.h to 'struct A;' and both headers can be included
together.

【在 S*******s 的大作中提到】
: header1.h:
: struct A{
: int a;
: };
: header2.h:
: class A;
: class B{
: A* pA;
: }
: 如果某cpp只包括这两个中的一个,都没问题。可是如果需要都包含,比如

h**********c
发帖数: 4120
5
As I understand, include "xxx.h" means copying all ines in "xxx.h" here.
So you need guard const.
#ifndef __xxx_h
#define __xxx_h
your stuff
#endif
l********a
发帖数: 1154
6
#include guard +1
X****r
发帖数: 3557
7
你们看明白楼主的问题了吗?

【在 l********a 的大作中提到】
: #include guard +1
1 (共1页)
进入Programming版参与讨论
相关主题
弱问c++里有没有NULL这个keyword?一道面试怪题C++. (转载)
c++,这种做法不行?typedef
又一个初级问题: C++中多如牛毛的#define格式c++ initialize struct
弱问C++一个问题 一直不解Question about friend in C++
怎样include一个函数请教有关header file的几个问题
ask a question about compile shared library using libtoolWhat is the difference between class and struct?
关于C++ STL编译的疑问C 里面有办法永久改变一个指针的属性吗?
基础问题:在header里面define functiongcc 编译的时候要包括 header source file 吗?
相关话题的讨论汇总
话题: class话题: forward话题: 包含话题: struct