我有3個類。顯示功能不起作用
class piesa_a{
protected:
int id;
char *tip;
int pret;
public:
[custructor with/without param, display function - works well each one of it]
class piesa_b:public piesa_a
{
private:
float lungime;
bool bw;
public:
[custructor with/without param, display function - works well each one of it]
class piesa_c:public piesa_a
{
private:
int nr;
piesa_b *buf;
public:
piesa_c():piesa_a(){nr=0; buf = new piesa_b[nr];}
piesa_c(int n, piesa_b *bu,int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz)
{
buf = new piesa_b[nr];
for(int i=0;i<nr;i++)
buf[i]= bu[i];
}
void afisare()
{
cout<<nr;
}
在主,我有這樣的:
piesa_c C(2, H,14,"TIPC",20);
C.afisare();
但是,這是行不通的。 我不知道「buf」是否正確申報,因爲問題似乎在上課。 爲什麼?
後來編輯: 整個代碼在這裏:http://pastebin.com/nx2FGSfe。
現在,我有這個主
int main(int argc, char** argv) {
piesa_b *H;
H = new piesa_b[2];
piesa_a A(4,"TIPA",120);
piesa_b B(100,1,3,"TIPA",120);
H[0]=B;
H[1]=B;
piesa_c C(2, H,14,"TIPC",20);
piesa_a** v = new piesa_a*[3];
v[0] = &A;
v[1] = &B;
v[2] = &C;
for(int i=0;i<3;i++)
v[i].afisare();
return 0;
}
顯示功能返回該錯誤
main.cpp:143:14: error: request for member ‘afisare’ in ‘*(v + ((unsigned int)(((unsigned int)i) * 4u)))’, which is of non-class type ‘piesa_a*’
什麼是'H'?你必須更具描述性,*它如何不能「工作」?它看起來好像它不應該建立,但你不包括任何你不瞭解的編譯器輸出...... – unwind
我沒有看到一個名字顯示在任何地方的函數...... – PlasmaHH
@PlasmaHH它被稱爲'afisare ()':) –