【问题标题】:Pointer to incomplete class type is not allowed不允许指向不完整类类型的指针
【发布时间】:2012-08-15 04:37:33
【问题描述】:

由于某种原因,我不能使用附加到我想使用的对象的函数。我在不起作用的行中添加了注释。作为一个错误,我得到“错误;不允许指向不完整类类型的指针”请帮助

这是dokter.ccp中的代码

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.CCP 中的代码

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

【问题讨论】:

  • 如果我在 .h 文件中转发声明该类并且未能在 .cpp 文件中包含该类的完整头文件,通常会出现此错误
  • 您是否包含了标题?您是否确保没有其他标头使用相同的包含保护 (WIELRENNER_H_)?这个类是叫Person 还是Persoon,还是你有两个名字的类?
  • -1 问题省略所有可以指出问题的代码。问题仍然很清楚(wielrennerOB 已被声明为指向不完整类型的指针),但不是由于 OP 的努力。在我写下 OP 更新并更改了问题之后,但我仍然不赞成:这是一团糟,并且将部分代码从一种自然语言翻译以使其更加“清晰”,天哪。
  • 您好像忘记在 dokter.cpp 中包含 wielrenner.h
  • @Cheersandhth.-Alf:我不认为他在翻译别人的源代码,我认为他为了我们的利益试图将自己的源代码翻译成英文。

标签: c++ list class pointers iterator


【解决方案1】:

“不完整的类”是一种已声明但未定义的类。例如

class Wielrenner;

相对

class Wielrenner
{
    /* class members */
};

你需要在dokter.ccp#include "wielrenner.h"

【讨论】:

  • 我已经完成了这堂 Wielrenner 课程;在 cpp 的头文件中(代码所在的位置)
  • @Sharpless512: dokter.ccp 里面需要#include "wielrenner.h",这样才能知道类的细节。看起来dokter.h 里面只有class Wielrenner;,因此是不完整的类错误。
  • 感谢您给我的想法找出头文件声明触发错误的函数并包含头文件
  • 请注意,如果您编写了错误的预声明名称并导入了该头文件,也会发生这种情况。一切似乎都很好,但预先声明不正确:-)
【解决方案2】:

要检查的一件事......

如果你的类被定义为 typedef:

typedef struct myclass { };

然后您尝试在其他任何地方将其称为struct myclass,您将左右出现不完整类型错误。忘记类/结构是类型定义的有时是错误的。如果是这种情况,请从以下位置删除“struct”:

typedef struct mystruct {}...

struct mystruct *myvar = value;

改为使用...

mystruct *myvar = value;

常见错误。

【讨论】:

    【解决方案3】:

    在错误的命名空间内声明前向引用时会出现此错误,从而声明新类型而不定义它。例如:

    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`
      }
    }
    

    【讨论】:

      【解决方案4】:

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

      我有一个这样的课程,当使用 .cpp 文件中的“事件”时出现错误。

      我的文件.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);
          };
      

      【讨论】:

        【解决方案5】:

        检查您是否缺少某些导入。

        【讨论】:

          猜你喜欢
          • 2021-12-31
          • 2015-10-09
          • 1970-01-01
          • 2017-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多