如果我在頭文件中聲明一個對象,則會出現編譯錯誤。但是,我可以在我的應用程序的setup()方法中構造它,只需調用Analyzer A(44100., "a", 200);
即可。包含類型的對象時出現問題
如果我這樣構造它,我該如何保留一個指針?一旦構造函數調用超出範圍,該對象是不可訪問的嗎?
或者,有沒有另外一種方法我應該得到這個對象的一個實例?
(。我已經習慣是把像Analyzer A;
在我的頭,然後在CPP把A = new Analyzer(44100., "a", 200);
此,雖然不會編譯)
Analyzer.hh:
class Analyzer {
public:
/// constructor
Analyzer(double rate, std::string id, std::size_t step = 200);
};
Analyzer.cc:
Analyzer::Analyzer(double rate, std::string id, std::size_t step):
m_step(step),
m_rate(rate),
m_id(id),
m_window(FFT_N),
m_bufRead(0),
m_bufWrite(0),
m_fftLastPhase(FFT_N/2),
m_peak(0.0),
m_oldfreq(0.0)
{
/* ... */
}
testApp.h:
#include "Analyzer.hh"
class testApp : public ofSimpleApp{
public:
// *This line gives compilation error
// "No matching function for call to Analyzer::Analyzer()"
Analyzer A;
}
testApp.cpp:
void testApp::setup(){
// *This line compiles, but how will I access
//this object outside of current scope?*
Analyzer A(44100., "a", 200);
}
* initialize *,not * define *,但這是正確的方法。 –
@ Ben Voigt謝謝 –