我有这个问题符号'A'无法在文件B.h中解析,我正在使用Eclipse IDE for C / C ++ Developers:
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
class B: public cs::A{
};
#endif
包括A.h文件:
//A.h file
#ifndef A_H_
#define A_H_
namespace cs{
class A {
};
}
#endif
我在这里缺少什么?
答案 0 :(得分:5)
您将类A
放在命名空间中,您应该在使用它时保持命名空间的解析:
class B: public cs::A{
};
或者
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
using namespace cs;
class B: public A{
};
#endif
不建议使用(查看Als的评论)。
此外,您可以这样做,以避免每次使用A
(您应该在第一个解决方案中执行)和using
所有命名空间时保持整个命名空间限定:
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
using cs::A;
class B: public A{
};
#endif
答案 1 :(得分:3)
class B: public cs::A{ };
^^^^^^
您需要提供类A
的完全限定名称。
请注意,类A
是在命名空间cs
中定义的,因此您不能在没有命名空间限定的情况下使用A
。
答案 2 :(得分:0)
您正在使用命名空间cs,请记住在声明B类时再次使用它。
//B.h file
#ifndef __B_H__
#define __B_H__
#include "A.h"
class B: public cs::A{
};
#endif