不允许指向不完整类类型的指针
Pointer to incomplete class type is not allowed
由于某些原因,我无法使用附加到要使用的对象的函数。我在这行添加了一条不起作用的评论。作为一个错误,我得到";错误不允许指向不完整类类型的指针";请帮助
这是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);
};
如果头文件没有明确包含在需要的地方,而是通过其他头文件隐式包含,也会出现问题。
检查是否缺少某些导入。
相关文章:
- 非类型指针和引用模板参数,以及在编译时如何/为什么解析它们.c++
- 如何访问在 c++ 中在类内声明的结构类型指针变量?
- (C )找到基本类型指针的儿童类型
- 表达式必须具有指向对象的指针类型(指针向量)
- 将子项复制构造到父类型指针中
- C 返回类型指针声明
- 调用虚拟函数而不通过类类型指针创建任何对象
- C++ 如何使用类类型指针制作向量
- 为什么基类型指针不能获取派生类对象的地址值?
- 如何在 C++ 中获取映射类型指针,映射
- 如何检查该类型的类型指针是正确对齐的
- 正在转换为短类型指针
- 指向任意类方法的模板非类型指针
- 如何通过强制转换类型指针将字符数组转换为uint16_t
- 指向shared_ptr的不透明类型 C 指针
- C++:从值类型指针强制转换为包含迭代器
- C/C++:访问给定类型指针的位置与访问另一类型指针的相同位置不同
- 比较类型指针
- 数据类型指针使用*(Datatype *)
- 如何打印c++中char类型指针的所有值