PDA

View Full Version : global variable in QT: getting ISSUES



Girija
19th September 2010, 15:37
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


#ifndef CITYBOOKGLOBALVARIABLES_H
#define CITYBOOKGLOBALVARIABLES_H
#include <QStringList>
class CityBookGlobalVariables
{
private:
CityBookGlobalVariables(){};
CityBookGlobalVariables( const CityBookGlobalVariables& _instance ){};
static CityBookGlobalVariables* m_pInstance;


public:

QStringList m_cityList;
static CityBookGlobalVariables* instance()
{
if ( !m_pInstance )
m_pInstance = new CityBookGlobalVariables;

return m_pInstance;
}

void setCityList(QStringList cityList)
{
m_cityList = cityList;
}

QStringList getCityList()
{
return m_cityList;
}
};

CityBookGlobalVariables* m_pInstance = 0;
#endif // CITYBOOKGLOBALVARIABLES_H


please help me. Thanks in advance

Lykurg
19th September 2010, 15:42
Put
CityBookGlobalVariables* m_pInstance = 0;outside your header file! And also please do note the
tags.

EDIT: also have a look at Singleton pattern.

Girija
19th September 2010, 15:46
Thanks. could you please tell little bit more?

Lykurg
19th September 2010, 15:54
could you please tell little bit more?
Regarding what?

Girija
19th September 2010, 16:01
Hi,, still I am getting the error and also getting lot of warnings. Am I on right way for using global varialbes?

Lykurg
19th September 2010, 16:09
Clean your project, rerun qmake. This may help. Otherwise there is a perfect working solution in your wiki: Singleton Pattern. 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?

Girija
19th September 2010, 16:27
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.

Lykurg
19th September 2010, 16:35
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.

SixDegrees
19th September 2010, 17:15
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.