当A对象的头文件中,有用到B对象,
而且B对象的头文件中,也有用到A对象,这2者同时满足时。
我们就要用到 头部类申明 class XXX;
e.g. 父子关系
<父>---------------------------------------------------------------------------
-
//
-
// father.h
-
//
-
-
#ifndef FATHER_H
-
#define FATHER_H
-
-
//
-
// Previous Declaration
-
//
-
class Son;
-
-
class Father
-
{
-
public:
-
Father(int age);
-
void pt();
-
void setSon(Son* s);
-
void ptSon();
-
private:
-
int m_age;
-
Son* m_Son;
-
};
-
-
#endif
-
//
-
// father.cpp
-
//
-
-
#include <iostream>
-
-
#include "father.h"
-
#include "son.h"
-
-
using namespace std;
-
-
Father::Father(int age)
-
: m_age(age)
-
, m_Son(nullptr)
-
{
-
}
-
-
-
void Father::pt()
-
{
-
cout << "I'm Father , I'm " << m_age << " years old. " << endl;
-
}
-
-
-
void Father::setSon(Son* s)
-
{
-
m_Son = s;
-
}
-
-
void Father::ptSon()
-
{
-
if( m_Son!=nullptr ) {
-
m_Son->pt();
-
}
-
}
<子>---------------------------------------------------------------------------
-
//
-
// son.h
-
//
-
-
#ifndef SON_H
-
#define SON_H
-
-
-
#include "father.h"
-
-
class Son
-
{
-
public:
-
Son(Father* pFa,int age);
-
void pt();
-
void ptFather();
-
private:
-
Father* m_pFather;
-
int m_sonAge;
-
};
-
-
#endif
-
//
-
// son.cpp
-
//
-
-
#include <iostream>
-
-
// #include "father.h"
-
#include "son.h"
-
-
using namespace std;
-
-
-
Son::Son(Father* pFa, int age)
-
: m_pFather(pFa)
-
, m_sonAge(age)
-
{
-
}
-
-
-
void Son::pt()
-
{
-
cout << "I'm Son , I'm " << m_sonAge << " years old. " << endl;
-
}
-
-
-
void Son::ptFather()
-
{
-
if( m_pFather!=nullptr ) {
-
m_pFather->pt();
-
}
-
}
这里注意 , son.h , son.cpp 有2种写法
1. 如上面的代码
2. 注释掉 son.h 第9行
#include "father.h"
然后取消注释 son.cpp 第7行 #include
"father.h"
!!!!! 请注意 son.cpp 中 #include "father.h" 一定要放在 #include "son.h" 之前 !!!!!
因为 在展开 son.h头文件后,会因为找不到 father类的定义,而编译失败
如果硬要把 #inlclude "father.h" 放在 #include "son.h" 之后,那么,就必须在 #include "son.h" 之前加前置类申明 , 添加此语句 class Father;
-
//
-
// main.cpp
-
//
-
-
#include <iostream>
-
using namespace std;
-
-
#include "father.h"
-
#include "son.h"
-
-
int main(int argc, char* argv[])
-
{
-
Father* pFa = new Father(50);
-
Son* pSon = new Son(pFa,20);
-
pFa->setSon(pSon);
-
-
pFa->pt();
-
pFa->ptSon();
-
-
cout << "---------------------------------------------" << endl;
-
-
pSon->pt();
-
pSon->ptFather();
-
-
//
-
// delete object
-
//
-
delete pFa;
-
pFa = nullptr;
-
delete pSon;
-
pSon = nullptr;
-
-
return 0;
-
}
附上 makefile
-
# makefile
-
-
-
all:main.o father.o son.o
-
g++ --std=c++11 -g3 -Wall main.o father.o son.o -o main
-
-
main.o:main.cpp father.h son.h
-
g++ --std=c++11 -g3 -Wall -o main.o -c main.cpp
-
-
father.o:father.h father.cpp
-
g++ --std=c++11 -g3 -Wall -o father.o -c father.cpp
-
-
son.o:son.h son.cpp
-
g++ --std=c++11 -g3 -Wall -o son.o -c son.cpp
-
-
.PHONY:clean
-
clean:
-
-rm -fR main.dSYM main.o father.o son.o main
------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------
<复杂情况>
在相互包含的类外面有 命名空间 时, 代码就难写很多,试了多次,都编译失败
编译错误写法枚举: (均在 father.h 头文件中修改的 )
1. 在 class Son; 行处修改成 --> class S::Son;
2. 在 class Son;行处之上添加 using namespace S一行;
3. 注释掉 class Son;行 , 在 namespace Fa { 后面一行添加 class Son;1行
4.
注释掉 class Son;行 , 在 namespace Fa { 后面一行添加 class S::Son;1行
5.
注释掉 class Son;行 , 在 namespace Fa { 后面一行添加 using namespace S; class Son;2行
6. 注释掉 class Son;行 , 在 namespace Fa { 后面一行添加 using namespace S;1行
7. 维护原来的写法
最终才知道,没有
class S::Son; 这种写法, class 后面直接,也只能接一个类名,不能在前面加命名空间
namespace S
{
class Son;
}
才是正确的写法
-
//
-
// father.h
-
//
-
-
#ifndef FATHER_H
-
#define FATHER_H
-
-
//
-
// Previous Declaration
-
//
-
namespace S
-
{
-
class Son;
-
}
-
-
-
namespace Fa
-
{
-
-
class Father
-
{
-
public:
-
Father(int age);
-
void pt();
-
void setSon(S::Son* s);
-
void ptSon();
-
private:
-
int m_age;
-
S::Son* m_Son;
-
};
-
-
} // end namespace Father
-
-
#endif
-
//
-
// father.cpp
-
//
-
-
#include <iostream>
-
-
#include "father.h"
-
#include "son.h"
-
-
using namespace std;
-
-
namespace Fa
-
{
-
-
Father::Father(int age)
-
: m_age(age)
-
, m_Son(nullptr)
-
{
-
}
-
-
-
void Father::pt()
-
{
-
cout << "I'm Father , I'm " << m_age << " years old. " << endl;
-
}
-
-
-
void Father::setSon(S::Son* s)
-
{
-
m_Son = s;
-
}
-
-
void Father::ptSon()
-
{
-
if( m_Son!=nullptr ) {
-
m_Son->pt();
-
}
-
}
-
-
-
} // end namespace Father
-
//
-
// son.h
-
//
-
-
#ifndef SON_H
-
#define SON_H
-
-
-
#include "father.h"
-
-
namespace S
-
{
-
-
class Son
-
{
-
public:
-
Son(Fa::Father* pFa,int age);
-
void pt();
-
void ptFather();
-
private:
-
Fa::Father* m_pFather;
-
int m_sonAge;
-
};
-
-
} // end namespace S
-
-
#endif
-
//
-
// son.cpp
-
//
-
-
#include <iostream>
-
-
-
// #include "father.h"
-
#include "son.h"
-
-
-
using namespace std;
-
-
namespace S
-
{
-
-
Son::Son(Fa::Father* pFa, int age)
-
: m_pFather(pFa)
-
, m_sonAge(age)
-
{
-
}
-
-
-
void Son::pt()
-
{
-
cout << "I'm Son , I'm " << m_sonAge << " years old. " << endl;
-
}
-
-
-
void Son::ptFather()
-
{
-
if( m_pFather!=nullptr ) {
-
m_pFather->pt();
-
}
-
}
-
-
} // end namespace S
-
//
-
// main.cpp
-
//
-
-
#include <iostream>
-
using namespace std;
-
-
#include "father.h"
-
#include "son.h"
-
-
-
int main(int argc, char* argv[])
-
{
-
Fa::Father* pFa = new Fa::Father(50);
-
S::Son* pSon = new S::Son(pFa,20);
-
pFa->setSon(pSon);
-
-
pFa->pt();
-
pFa->ptSon();
-
-
cout << "---------------------------------------------" << endl;
-
-
pSon->pt();
-
pSon->ptFather();
-
-
//
-
// delete object
-
//
-
delete pFa;
-
pFa = nullptr;
-
delete pSon;
-
pSon = nullptr;
-
-
return 0;
-
}
阅读(2052) | 评论(0) | 转发(0) |