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 | |
X****r 发帖数: 3557 | 7 你们看明白楼主的问题了吗?
【在 l********a 的大作中提到】 : #include guard +1
|