不允许指向不完整类类型的指针

Pointer to incomplete class type is not allowed

本文关键字:类型 指针 不允许      更新时间:2023-10-16

由于某些原因,我无法使用附加到要使用的对象的函数。我在这行添加了一条不起作用的评论。作为一个错误,我得到";错误不允许指向不完整类类型的指针";请帮助

这是dokter.cpp 中的代码

    int counter = 0;        
    for (list<Wielrenner*>::iterator it = wielrenners.begin(); it != wielrenners.end(); it++){
        Wielrenner* wielrennerOB = *it;
        cout << "nID: " << counter;
        cout << "List size: " << persons.size() << endl;
        
        wielrennerOB->print();  // This is not working
        counter++;
     }  

这是wielrenner.h 中的代码

    #ifndef WIELRENNER_H_
    #define WIELRENNER_H_
    //#include <fstream>
 
    #include "persoon.h"
    #include "Onderzoek.h"
    class Wielrenner :
    public Persoon
    {
    public:
        Wielrenner(string, string, Adres, string, Datum, Datum, string, int, float, float, float,list<Onderzoek>* );
        ~Wielrenner(void);
        int     getLengte() const;
        float   getGewicht() const;
        float   getVo2max() const;
        float   getMaxVermogen() const;
        list<Onderzoek> getOnderzoekenList();
        void    setLengte(int);
        void    setGewicht(float);
        void    setVo2max(float);
        void    setMaxVermogen(float);
        void    voegOnderzoekToeList(Onderzoek);
        void    showOnderzoeksList();
        void    setOnderzoeksLijst(list<Onderzoek>&);
        void    print();
        void    printFile(ofstream&);

    private:
    int     lengte;
    float   gewicht;
    float   vo2max;
    float   maxVermogen;
    list<Onderzoek> onderzoeken;
    };
    #endif /* WIELRENNER_H_ */

wielrenner.cpp 中的代码

    using namespace std;
    #include <string>
    #include "Wielrenner.h"
    /*
    #include "Onderzoek.h"
    */
    Wielrenner::Wielrenner(string voornaam, string achternaam, Adres adres, string telefoon, Datum datumInDienst, Datum geboorteDatum, 
                        string persoonType, int lengte, float gewicht, float vo2max, float maxVermogen,list<Onderzoek>* onderzoeken)
            : lengte(lengte), 
        gewicht(gewicht), 
        vo2max(vo2max), 
        maxVermogen(maxVermogen),
        Persoon(voornaam, achternaam, adres, telefoon, datumInDienst, geboorteDatum, persoonType)
    {
    }

    Wielrenner::~Wielrenner(void)
    {
    }
    //setten van gegevens
    void    Wielrenner::setLengte(int newLengte){
    lengte = newLengte;
    }
    void    Wielrenner::setGewicht(float newGewicht){
    gewicht = newGewicht;
    }
    void    Wielrenner::setVo2max(float newVo2max){
    vo2max = newVo2max;
    }
    void    Wielrenner::setMaxVermogen(float newMaxVermogen){
    maxVermogen = newMaxVermogen;
    }
    void    Wielrenner::voegOnderzoekToeList(Onderzoek newOnderzoek){
    onderzoeken.push_back(newOnderzoek);            
    }
    void    Wielrenner::showOnderzoeksList(){
    int teller=0;
    
    for (list<Onderzoek>::iterator it = onderzoeken.begin(); it != onderzoeken.end();     it++){
        Onderzoek onderzoekOB = *it;
        cout << teller << " - ";
        onderzoekOB.print();
        teller++;
     }  
    }
    void    Wielrenner::setOnderzoeksLijst(list<Onderzoek>& newOnderzoeksLijst){
    onderzoeken = newOnderzoeksLijst;
    }
    void    Wielrenner::print(){
    cout << "(" << persoonID << ") Persoon: " << endl;
    cout << persoonType << endl;
    cout << voornaam << " " << achternaam << endl;
    adres.print();
    cout << telefoon << endl;
    cout << "Datum in dienst: ";
    datumInDienst.print();
    cout << "Geboortedatum: ";
    geboorteDatum.print();
    cout << "> Extra wielrenner gegevens: " << endl;
    cout << "Lengte: " << lengte << endl;
    cout << "Gewicht: " << gewicht << endl;
    cout << "vo2max: " << vo2max << endl;
    cout << "maxVermogen: " << maxVermogen << endl;
    }
    void Wielrenner::printFile(ofstream &myfile){
    myfile <<  persoonID << "n";
    myfile << persoonType << "n";
    myfile << voornaam << " " << achternaam << "n";
    adres.printFile(myfile);
    myfile << telefoon << "n";
    datumInDienst.printFile(myfile);
    geboorteDatum.printFile(myfile);
    myfile << lengte << "n";
    myfile << gewicht << "n";
    myfile << vo2max << "n";
    myfile << maxVermogen << "n";
    }
    // returnen van gegevens
    int     Wielrenner::getLengte() const{
    return lengte;
    }
    float   Wielrenner::getGewicht() const{
    return gewicht;
    }
    float   Wielrenner::getVo2max() const{
    return vo2max;
    }   
    float   Wielrenner::getMaxVermogen() const{
    return maxVermogen;
    }
    list<Onderzoek> Wielrenner::getOnderzoekenList(){
    return onderzoeken;
    }

"不完整类"是一个已声明但未定义的类。例如

class Wielrenner;

与相反

class Wielrenner
{
    /* class members */
};

您需要在dokter.ccp 中使用#include "wielrenner.h"

有一件事需要检查。。。

如果你的类被定义为typedef:

typedef struct myclass { };

然后,在其他任何地方尝试将其称为struct myclass,您都会左右出现不完整类型错误。有时忘记类/结构是typedef'ed是错误的。如果是这种情况,请从中删除"struct"

typedef struct mystruct {}...
struct mystruct *myvar = value;

请改用。。。

mystruct *myvar = value;

常见错误。

在错误的命名空间中声明前向引用时会出现此错误,从而在没有定义的情况下声明新类型。例如:

namespace X
{
  namespace Y
  {
    class A;
    void func(A* a) { ... } // incomplete type here!
  }
}

但是,在A类中是这样定义的:

namespace X
{
  class A { ... };
}

因此,A被定义为X::A,但我使用它作为X::Y::A

修复方法显然是将前向引用移动到适当的位置,如下所示:

namespace X
{
  class A;
  namespace Y
  {
    void func(X::A* a) { ... } // Now accurately referencing the class`enter code here`
  }
}

我遇到了同样的问题,并通过检查#includes解决了它。如果你使用QKeyEvent,你必须确保你也包括它。

我有一个这样的类,在.cpp文件中处理"event"时出现了错误。

myfile.h

    #include <QKeyEvent> // adding this import solved the problem.
    class MyClass : public QWidget
    {
      Q_OBJECT
    public:
      MyClass(QWidget* parent = 0);
      virtual ~QmitkHelpOverlay();
    protected:
        virtual void  keyPressEvent(QKeyEvent* event);
    };

如果头文件没有明确包含在需要的地方,而是通过其他头文件隐式包含,也会出现问题。

检查是否缺少某些导入。