无效使用不完整类型'class classname'C ++

时间:2019-03-01 00:59:56

标签: c++ declaration circular-dependency forward-declaration

一个.h

#pragma once
class Three;

class One
{
    public:
      std::string name;
      One(std::string newName);
      virtual void Myfunction(Three *three) = 0;
}

One.cpp

#include "One.h"

One::One(std::string newName)
{
      name = newName;
}

Two.h

#pragma once
#include "One.h"

class Two : public One
{
   public:
        Two(std::string newName);
        void Myfunction(Three * three);
}

Two.cpp

#include "Two.h"

Two::Two(std::string newName)
        :One(newName)
{};

void Two::Myfunction(Three *three)
{
    std::cout << three->newnum << std::endl;
}

Three.h

 #pragma once
 #include "One.h"

 class Three
 {
     public:
          One *one;
          int num;
          Three(One *newone, int newnum);
 }

Three.cpp

 #include "Three.h"

 Three(One *newone, int newnum)
     :one(newone),
      num(newnum)
 {     
 }

因此,我有三个类,一个是两个的父类,而两个则覆盖了纯虚函数。三个持有指向一个对象的指针,我想要一个指向三分之二的指针,因此当使更多类从一个继承时,我可以使用多态性,以使一个指针等于作为One的子代的新对象,并为所有这些对象打印num从三个指针。当我编译时,我得到

  错误:错误地使用了不完整的类型“三类”   std :: cout <<三-> num << std :: endl;

我知道我有循环依赖,我只是停留在如何解决它上。

0 个答案:

没有答案