global variable in QT: getting ISSUES
hi,
I am using following code for making global variable. But I am getting the error:
:: error: duplicate symbol _m_pInstance in citybook.o and main.o
:: error: collect2: ld returned 1 exit status
My code : CityBookGlobalVariables.h
Code:
#ifndef CITYBOOKGLOBALVARIABLES_H
#define CITYBOOKGLOBALVARIABLES_H
#include <QStringList>
class CityBookGlobalVariables
{
private:
CityBookGlobalVariables(){};
CityBookGlobalVariables( const CityBookGlobalVariables& _instance ){};
static CityBookGlobalVariables* m_pInstance;
public:
static CityBookGlobalVariables* instance()
{
if ( !m_pInstance )
m_pInstance = new CityBookGlobalVariables;
return m_pInstance;
}
{
m_cityList = cityList;
}
{
return m_cityList;
}
};
CityBookGlobalVariables* m_pInstance = 0;
#endif // CITYBOOKGLOBALVARIABLES_H
please help me. Thanks in advance
Re: global variable in QT: getting ISSUES
Put
Code:
CityBookGlobalVariables* m_pInstance = 0;
outside your header file! And also please do note the [CODE][/CODE] tags.
EDIT: also have a look at [WIKI]Singleton pattern[/WIKI].
Re: global variable in QT: getting ISSUES
Thanks. could you please tell little bit more?
Re: global variable in QT: getting ISSUES
Quote:
Originally Posted by
Girija
could you please tell little bit more?
Regarding what?
Re: global variable in QT: getting ISSUES
Hi,, still I am getting the error and also getting lot of warnings. Am I on right way for using global varialbes?
Re: global variable in QT: getting ISSUES
Clean your project, rerun qmake. This may help. Otherwise there is a perfect working solution in your wiki: [WIKI]Singleton Pattern[/WIKI]. If that then still don't work for you, please provide a minimal example reproducing your problem.
And how do you use CityBookGlobalVariables in you other code?
Re: global variable in QT: getting ISSUES
Hi,
Actually, The problem is...
I am trying to do store the some set of data in QStringList from one of my class. And get the value of QStringList from another class. So I want to make the QStringList as a global variable. But Still I am getting the same error.
Please help me.
Thanks in advance.
Re: global variable in QT: getting ISSUES
Quote:
Originally Posted by
Girija
Please help me.
I can not help you, if you do not provide a minimal compilable program which reproduces your problem or simply show you other code.
Re: global variable in QT: getting ISSUES
You must DECLARE a global variable in the header file, but you must DEFINE it only once, in a compilable source (*.cpp) file. You're defining it in the header, and you're going to get multiple definition errors because there are several files that include that header.
Stuff "C++ global variables" into Google if you don't understand the difference and where to place each in your code.
Or, as suggested, look at the Singleton pattern, although this is often overkill when a simple global will do.