2011-12-03 74 views
-2

有類的定義是這樣的:「this」 指針(C++)

template <class Impl> 
FullO3CPU<Impl>::FullO3CPU(DerivO3CPUParams *params) 
class DerivO3CPU : public FullO3CPU<O3CPUImpl> 
{ 
    public: 
    DerivO3CPU(DerivO3CPUParams *p) 
     : FullO3CPU<O3CPUImpl>(p) 
    { } 
}; 

DerivO3CPU * 
DerivO3CPUParams::create() 
{ 
    ... 
    return new DerivO3CPU(this); 
} 

我改變是增加另一個參數FullO3CPU

template <class Impl> 
FullO3CPU<Impl>::FullO3CPU(DerivO3CPUParams *params, My_param *mp) 

class DerivO3CPU : public FullO3CPU<O3CPUImpl> 
{ 
    public: 
    DerivO3CPU(DerivO3CPUParams *p, My_param *mp) 
     : FullO3CPU<O3CPUImpl>(p, mp) 
    { } 
}; 

DerivO3CPU * 
DerivO3CPUParams::create() 
{ 
    ... 
    return new DerivO3CPU(this); 
} 

但是我不知道該怎麼辦

return new DerivO3CPU(this); 

,因爲我得到這個錯誤:

error: no matching function for call to 'DerivO3CPU::DerivO3CPU(DerivO3CPUParams* const)' 
note: candidates are: 
note: DerivO3CPU::DerivO3CPU(DerivO3CPUParams*, My_param*) 
note: candidate expects 2 arguments, 1 provided 
+0

我不明白你的問題。你已經在構造函數中添加了第二個參數,現在你在問怎麼處理它? –

+0

不,如何處理錯誤 – mahmood

+1

@mahmood,你正在調用一個參數函數...你被_just_銷燬了。如果您想要編譯該代碼,請提供一個參數的構造函數。如果您嘗試使用7個參數調用構造函數,並且以前沒有創建它,則它也會產生編譯錯誤。 –

回答

2

那麼,你也需要把它加到create,或者使用一個存儲在某個地方的東西。

DerivO3CPU * 
DerivO3CPUParams::create(My_param *p) 
{ 
    ... 
    return new DerivO3CPU(this, p); 
} 
1

什麼功能是你試圖打電話? DerivO3CPU的構造函數沒有一個參數。有一個,但你添加了第二個參數。那麼它怎麼可能工作?您必須保留一個參數構造函數,或停止調用它。

0

你試圖調用DerivO3CPU的構造函數,唯一可用的構造是

DerivO3CPU(DerivO3CPUParams *p, My_param *mp) 

所以,你應該做的是要麼提供My_param *在你的「新」的呼叫,或建立另一個構造不需要My_param *參數,但爲FullO3CPU調用提供了一些默認值。