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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12027656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:47:39  来源:igfitidea点击:

Pointer to incomplete class type is not allowed

c++listclasspointersiterator

提问by Sharpless512

For some reason I cannot use functions attached to the object I want to use. I added a comment to the line that is not working. As an error I get "Error; pointer to incomplete class type is not allowed" Please help

出于某种原因,我无法使用附加到我想要使用的对象的函数。我在不起作用的行中添加了一条评论。作为错误,我收到“错误;不允许指向不完整类类型的指针”请帮助

This is code in dokter.ccp

这是 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++;
 }  

This is code in wielrenner.h

这是 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_ */

code in wielrenner.CCP

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

回答by KRyan

An "incomplete class" is one declared but not defined. E.g.

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

class Wielrenner;

as opposed to

class Wielrenner
{
    /* class members */
};

You need to #include "wielrenner.h"in dokter.ccp

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

回答by Bryan Wilcutt

One thing to check for...

一件事要检查...

If your class is defined as a typedef:

如果您的类被定义为 typedef:

typedef struct myclass { };

Then you try to refer to it as struct myclassanywhere else, you'll get Incomplete Type errors left and right. It's sometimes a mistake to forget the class/struct was typedef'ed. If that's the case, remove "struct" from:

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

typedef struct mystruct {}...

struct mystruct *myvar = value;

Instead use...

而是使用...

mystruct *myvar = value;

Common mistake.

常见的错误。

回答by Jeff Weichers

You get this error when declaring a forward reference inside the wrong namespace thus declaring a new type without defining it. For example:

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

namespace X
{
  namespace Y
  {
    class A;

    void func(A* a) { ... } // incomplete type here!
  }
}

...but, in class A was defined like this:

...但是,在 A 类中定义如下:

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

Thus, A was defined as X::A, but I was using it as X::Y::A.

因此, A 被定义为X::A,但我将其用作X::Y::A

The fix obviously is to move the forward reference to its proper place like so:

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

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

回答by zeroDivider

Check out if you are missing some import.

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

回答by Matthis Kohli

I came accross the same problem and solved it by checking my #includes. If you use QKeyEvent you have to make sure that you also include it.

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

I had a class like this and my error appeared when working with "event"in the .cpp file.

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

myfile.h

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