---
title: "c++学习：记一例namespace和类型声明引发的编译问题"
author: "ah_fu"
date: "2008-01-22T10:14:00Z"
tags: ["C", "C#", "C++", "MySpace"]
slug: "c-namespace"
draft: false
lastmod: "2008-01-22T10:14:00Z"
id: 281569
url: "https://ah-fu.iteye.com/blog/281569"
---

编码中发现一个奇怪的编译错误，花了很长时间才找到问题所在。下面把重现这个问题的最精简的代码贴出来：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)MyClass.h  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)存在一个namespace和一个类  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#ifndef \_MY\_CLASS\_H_  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#define \_MY\_CLASS\_H_  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)namespace MySpace  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)class MyClass  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)public:  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    int Field;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#endif  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

    下面的一个文件声明一个全局的指针，因为仅仅指示声明，所以尽量不包含其他文件：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)global.h  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)声明一个全局的指针  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#ifndef \_GLOBAL\_H_  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#define \_GLOBAL\_H_  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)class MyClass;           //对存在的类型做一个声明，不引入定义的头文件  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)extern MyClass* g\_Obj;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#endif  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

    下面的文件使用全局变量和类型：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)use.cpp  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)这个文件里面使用全局对象指针和类型  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include "MyClass.h"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)#include "global.h"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void test1()  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    g\_Obj = new MyClass();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    g\_Obj-&gt;Field = 1;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)/\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)g++ -o use.o -c use.cpp -g -Wall  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

    编译use.cpp，产生了很多编译错误：

only\_use.cpp: In function \`void test1()':  
only\_use.cpp:11: error: invalid use of undefined type \`struct MyClass'  
global.h:9: error: forward declaration of \`struct MyClass'  
only\_use.cpp:12: error: invalid use of undefined type \`struct MyClass'  
global.h:9: error: forward declaration of \`struct MyClass'

    你可能会说，得加上using namespace MySpace;  实际上，这句加进去还是编译不过。当然，如果在global.h中包含MyClass.h，应该就能够编译过去了。但是我不想这么做，对于一个指针类型，其类型只要声明就好，不需要引入定义的。包含太多的头文件会引发编译问题的。  
       试了很多办法，最后发现，在use.cpp中的#include "MyClass.h"后加入一行：using MySpace::MyClass; 然后代码就能够编译过去了。

     总结一下：对于存在于namespace中的类型，如果在某个头文件中对类型声明过，则在使用的时候要注意：  
1、类型的定义文件包含在前（#include "MyClass.h"）  
2、声明namespace中的类型（using MySpace::MyClass;）  
3、然后包含全局指针的声明文件（#include "global.h"）  
4、在其后使用就没问题了