Solved How to define static members in a static function in .cpp file?
-
@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 Not really what I asked...
Did you adddouble AmpConfiguration::sampling_rate;
to your cpp file?
-
@jsulm said in How to define static members in a static function in .cpp file?:
double AmpConfiguration::sampling_rate;
double AmpConfiguration::sampling_rate=10.2
//like this? in the static function? -
@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()
-
@jsulm yes bro, I did add like this
double AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
but new error camequalified-id decalration before "=" token
-
@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. -
@jsulm said in How to define static members in a static function in .cpp file?:
Where do you call this now?
In the
mainwindow.cpp
-
@thippu said in How to define static members in a static function in .cpp file?:
In the mainwindow.cpp
WHERE EXACTLY?
-
@jsulm
In the constructor of mainwindow.cpp
I'm callingAmpConfiguration::ReadIni();
First statement itself -
@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
. -
@jsulm said in How to define static members in a static function in .cpp file?:
AmpConfiguration::sampling_rate=line_list->at(0).toDouble();
No, change in the error. still, I'm getting undefined reference
-
@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++. -
@jsulm said in How to define static members in a static function in .cpp file?:
// This has to be OUTSIDE of any method/constructor
Worked. bro
Thank you