无效使用不完整类型"class ..." [英] Invalid use of incomplete type "class..."
本文介绍了无效使用不完整类型"class ..."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直收到此错误,但我不明白我做错了什么:方法 getAuftraeg()中的错误:无效使用了不完整的类型' Auftrag类'" >我要在其中打印" vpa "列表的位置.请帮我!!
I keep getting this error and I don't understand what I do wrong: " error: invalid use of incomplete type 'class Auftrag'" at method getAuftraeg() where I want to print the "vpa" list. Please help me!!
#include <iostream>
#include <vector>
using namespace std;
class Auftrag;
class Kunde {
public:
Kunde(Kunde *kunde) {
*this = kunde;
}
Kunde(string s) : name{s}, vpa{} {}
void print(){
cout<<this->name<<endl;
getAuftraege();
}
string getName() const { return name; }
void addAuftrag(Auftrag *auftrag){
this->vpa.push_back(auftrag);
}
void getAuftraege() {
for (int i{0};i<vpa.size();i++)
cout << this->vpa.at(i)->getID();
}
private:
string name;
vector<Auftrag*> vpa;
};
class Auftrag {
public:
Auftrag(string s) : id{s}, pk{nullptr} {}
void print() {
cout<<this->id<<endl;
}
string getID() const { return id; }
void setKunde(Kunde *kunde){
this->pk = kunde;
}
Kunde *getKunde() const;
private:
string id;
Kunde *pk;
};
int main() {
// Anlegen: zwei Kunden und drei Auftraege
Kunde *k1{new Kunde{"Sepplhuber-Finsterwalder"}};
Kunde k2{"Kurz"};
Auftrag *a1{new Auftrag{"Decke streichen"}};
Auftrag *a2{new Auftrag{"Wand tapezieren"}};
Auftrag a3{"Jalousie montieren"};
Auftrag a4{"Laminat verlegen"};
// Erste Assoziationsrichtung:
// aus einem Objekt vom Typ Kunde
// zu Objekten vom Typ Auftrag
k1->addAuftrag(a1);
k1->addAuftrag(a2);
k1->addAuftrag(&a3);
// Zweite Assoziationsrichtung:
// aus einem Objekt vom Typ Auftrag
// zu einem Objekt vom Typ Kunde
a4.setKunde(&k2);
// Alles ausgeben:
k1->print();
k2.print();
a1->print();
a2->print();
a3.print();
a4.print();
// Speicher zurueckgeben:
delete k1;
k1 = nullptr;
delete a1;
a1 = nullptr;
delete a2;
a2 = nullptr;
}
推荐答案
将函数定义放在类Auftrag
的定义之后.
Place the function definitions after the definition of the class Auftrag
.
例如
inline void Kunde ::addAuftrag(Auftrag *auftrag){
this->vpa.push_back(auftrag);
}
inline void Kunde ::getAuftraege() {
for (int i{0};i<vpa.size();i++)
cout << this->vpa.at(i)->getID();
}
编译器必须知道类Auftrag
的定义,才能确定其具有诸如vpa
这样的数据成员.
The compiler has to know the definition of the class Auftrag
that to determine that it has such a data member as vpa
.
这篇关于无效使用不完整类型"class ..."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文