从类继承,在不同的命名空间中定义

Yak*_*kov 5 c++ inheritance pointers namespaces

我有 2 个类,在不同的命名空间中定义:

//--==file1.hpp==--
namespace n1{
class x1 {
//.....
};
};
//--==file2.hpp==--
namespace n2{
class x1: public n1::x1{
//.....
    };
};

//--== file3.hpp ==--
namespace n2 {
 class x2 {
    private:
      n1::x1* data1_;
    public:
      void func(x1* data2) { data1_ = data2; }
  };
};
Run Code Online (Sandbox Code Playgroud)

这个编译失败

error C2440: '=' : cannot convert from `'n2::x1 *' to 'n1::x1 *'`
Run Code Online (Sandbox Code Playgroud)

我不明白什么是问题,因为 n2:x1 继承自 n1::x1...?谢谢

pha*_*ria -1

在 file2.h 中:包含 file1.h

在file3.h中:包含file1.h和file2.h。

并在 main 中包含 file3.h。

int main() {

n2::x2 xx22;
n2::x1* xx11;
xx22.func(xx11);
Run Code Online (Sandbox Code Playgroud)

}

这符合要求。