Sha*_*512 51 c++ pointers iterator class list
出于某种原因,我无法使用附加到我想要使用的对象的函数.我在不起作用的行中添加了注释.作为一个错误,我得到"错误;不允许指向不完整类类型的指针"请帮助
这是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++;
}
Run Code Online (Sandbox Code Playgroud)
这是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_ */
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
KRy*_*yan 101
"不完整的类"是声明但未定义的类.例如
class Wielrenner;
Run Code Online (Sandbox Code Playgroud)
而不是
class Wielrenner
{
/* class members */
};
Run Code Online (Sandbox Code Playgroud)
你需要#include "wielrenner.h"进来dokter.ccp
小智 6
要检查一件事......
如果您的类被定义为typedef:
typedef struct myclass { };
Run Code Online (Sandbox Code Playgroud)
然后你尝试struct myclass在其他任何地方引用它,你会得到左,右不完全类型的错误.忘记class/struct是typedef'有时是错误的.如果是这种情况,请从以下位置删除"struct":
typedef struct mystruct {}...
struct mystruct *myvar = value;
Run Code Online (Sandbox Code Playgroud)
而是使用......
mystruct *myvar = value;
Run Code Online (Sandbox Code Playgroud)
常见的错误.
小智 5
在错误的命名空间中声明前向引用时会出现此错误,从而在不定义新类型的情况下声明新类型.例如:
namespace X
{
namespace Y
{
class A;
void func(A* a) { ... } // incomplete type here!
}
}
Run Code Online (Sandbox Code Playgroud)
......但是,在A类中定义如下:
namespace X
{
class A { ... };
}
Run Code Online (Sandbox Code Playgroud)
因此,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`
}
}
Run Code Online (Sandbox Code Playgroud)