符号'A'无法解析

nab*_*bil 2 c++ eclipse oop header eclipse-cdt

我有这个问题符号'A'无法在文件Bh中解析,我正在使用Eclipse IDE for C/C++ Developers:

//B.h file

#ifndef __B_H__
#define __B_H__

#include "A.h"



class B:  public cs::A{

};

#endif
Run Code Online (Sandbox Code Playgroud)

包括Ah文件:

//A.h file

#ifndef A_H_
#define A_H_
namespace cs{
class A {


};
}

#endif
Run Code Online (Sandbox Code Playgroud)

我在这里缺少什么?

Tam*_*ash 5

您将该类A放在命名空间中,您应该在使用它时保持命名空间的解析:

class B:  public cs::A{

};
Run Code Online (Sandbox Code Playgroud)

要么

//B.h file

#ifndef __B_H__
#define __B_H__

#include "A.h"

using namespace cs;

class B:  public A{

};

#endif
Run Code Online (Sandbox Code Playgroud)

不建议使用(查看Als的评论).

此外,您可以这样做,以避免每次使用时保留整个命名空间限定A(您应该在第一个解决方案中执行),以及using所有命名空间:

//B.h file

#ifndef __B_H__
#define __B_H__

#include "A.h"

using cs::A;

class B:  public A{

};

#endif
Run Code Online (Sandbox Code Playgroud)

  • `using namespace cs;`肯定是一种矫枉过正,特别是在头文件中应该避免使用它.它会在每个包含头文件的翻译单元中导入名称空间`cs`中的所有符号. (3认同)