How to define static members in a static function in .cpp file?
-
Hi,
- I have declared
static double sampling_rate;
in ampconfigure.h
2)I have static member function asReadIni()
in ampconfigure.cpp
3)I did initializesampling_rate
inReadIni()
but getting error like undefined reference toAmpConfiguration::sampling_rate
. - How to initialize it properly?.
- I have declared
-
@thippu Please show your code (how you declare the static variable and how you're initialising it).
It sounds like you did not define the static member. You need this in your cpp:double AmpConfiguration::sampling_rate;
-
@jsulm okay.
ampconfiguration.h
static double sampling_rate; int getSampling_rate()const; void setSampling_rate(int value); static void ReadIni();
ampconfiguration.cpp
void AmpConfiguration::ReadIni() { QFile fp("AmpConfiguration.txt"); if(!fp.open(QIODevice::ReadOnly) { QMessageBox::information::(nullptr,"file error","file not found",QMessageBox::Ok); } QStringList *line_list=new QStringList; QTextStream txt_stream(&fp); while(!txt_stream.atEnd()) { QString line=txt_stream.readLine(); QStringList temp=line.split("=",QString::SkipEmptyParts); line_list->push_back(temp.at(1)); } sampling_rate=line_list->at(0).toDouble(); } //I am getting error in the building itself not on the runtime.
-
@thippu said in How to define static members in a static function in .cpp file?:
in the static function?
No. Like this:
double AmpConfiguration::sampling_rate=10.2; void AmpConfiguration::ReadIni()
-
@thippu said in How to define static members in a static function in .cpp file?:
double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
Please read again what I wrote.
Where do you call this now?
It looks like you're now trying to declare a LOCAL variable with same name which is for sure not what you want to do. -
@thippu said in How to define static members in a static function in .cpp file?:
In the mainwindow.cpp
WHERE EXACTLY?
-
@thippu Can you actually be more precise when answering questions?
Do you mean you put that line in the constructor or inside ReadIni()?
But as I said: you're declaring a local variable with same name.
It must be like this:AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
AmpConfiguration:: can be removed as well...
-
@jsulm said in How to define static members in a static function in .cpp file?:
local variable
I would like it to be a global static variable.
and I would like to use it(initialize) inside
AmpConfiguration::ReadIni()
@jsulm said in How to define static members in a static function in .cpp file?:
AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
finally, Calling the
AmpConfiguration::ReadIni()
in constructor ofmainwindow.cpp
. -
@thippu "I would like it to be a global static variable." - then why do you declare it as local variable?
Did you putdouble AmpConfiguration::sampling_rate=10.2; // This has to be OUTSIDE of any method/constructor void AmpConfiguration::ReadIni() { sampling_rate=line_list->at(0).toDouble(); }
as I already wrote above?
Please read about static members in C++.