Class Inheritance Defined in Different Namespaces

I have 2 classes defined in different namespaces:

//--==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; }
  };
};

Compiling this fails with

error C2440: '=' : cannot convert from `'n2::x1 *' to 'n1::x1 *'`

I can't figure out what could be the problem, since n2: x1 inherits from n1 :: x1 ...? Thanks you

+3
source share
2 answers

Inheritance from one namespace to another namespace class should not have any compilation error. Just in a subclass, if you need to call the method of the parent class (which is in a different namespace), you must use the full name (with namespace ).

For reference:

namespace a
{
class A1 {
 public:
    void testA1() {...}
};
}

namespace b
{
class B1: public class a::A1
{
 public:
    void testB1()
        {
          a::A1::testA1();
          ...
        }
};
}

, . , , .

+1

2.h: file1.h

3.h: file1.h file2.h.

include file3.h.

int main() {

n2::x2 xx22;
n2::x1* xx11;
xx22.func(xx11);

}

.

-1

All Articles