2011-11-09 55 views
0

我有休耕類:添加構件的boost :: ptr_vector <>

class CpuUsage { 
public: 
    CpuUsage(); 
    virtual ~CpuUsage(); 

    void SetCpuTotalTime(CpuCore _newVal); 
    CpuCore GetCpuTotalTimes(); 

    void AddSingleCoreTime(CpuCore& newval); 
private: 
    CpuCore total; 
    boost::ptr_vector<CpuCore> cpuCores; 
}; 

class CpuCore { 

public: 
    CpuCore(); 
    CpuCore(int _coreId, long _user, long _nice, long _sysmode, 
     long _idle, long _iowait, long _irq, long _softirq, long _steal, 
     long _guest); 

//all variable declarations... 
} 

對於添加CpuCore對象到cpuCores矢量,我要補充的指針?或者,我可以複製的價值,normaly,如:

void CpuUsage::AddSingleCoreTime(CpuCore _newVal) { 
    cpuCores.push_back(_newVal); 
} 

隨着CpuCore * _newVal參數,我有以下錯誤:
../src/usage/CpuUsage.h:42:錯誤:「升壓:: ptr_vector> CpuUsage :: cpuCores'是私人的 ../src/NodeInfoGather.cpp:73:錯誤:在這種情況下

什麼問題的矢量是私人在這裏?

感謝,

+0

您應該添加一個指針,'boost :: ptr_vector <>'擁有指針及其指向的內容。爲什麼不使用'std :: vector <>'? –

+0

我在這篇文章中使用了boost ptr向量:http://stackoverflow.com/questions/2693651/c-vector-of-objects-vs-vector-of-pointers-to-new-objects –

+1

你不似乎在給定的代碼上具有多於'CpuCore'的多態性。然而,發佈的代碼並不真實,「AddSingleCoreTime」的聲明和定義不同。 –

回答

0

你有一個指針到ptr_vector。請注意,它會採取指針的所有權,所以只是做

cpuCores.push_back(&_newVal); 

可能嚴重搞砸。如果你真的想要它(這不是你的問題),你可以實現virtual constructor

+0

不錯,它確實有效。但是我會修改我的架構,也許我真的不需要指針向量 - 我仍然必須理解C++的所有權 –

相關問題