Qt中的全局变量

时间:2010-09-19 19:10:32

标签: qt singleton global-variables

我想在Qt中创建一个全局变量。所以我写了一个单例类。但我收到以下错误

::错误:未找到符号,

:: error:collect2:ld返回1退出状态

我使用以下代码:

CityBookGlobalVariables.h:

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);
     QStringList getCityList();    
};

CityBookGlobalVariables.cpp:

//CityBookGlobalVariables* CityBookGlobalVariables::m_pInstance = 0;
CityBookGlobalVariables* m_pInstance = 0;
CityBookGlobalVariables::CityBookGlobalVariables()
{
}

CityBookGlobalVariables::CityBookGlobalVariables(const CityBookGlobalVariables& _instance ) 
{    
}

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

QStringList CityBookGlobalVariables::getCityList()
{
  return  m_cityList;
}

从A1.cpp调用setCityList

CityBookGlobalVariables *pointer = CityBookGlobalVariables::instance();
pointer->setCityList(cityitems);

从B1.cpp调用getCityList

CityBookGlobalVariables *pointer = CityBookGlobalVariables::instance();
cityModel->setStringList(pointer->getCityList()); 

0 个答案:

没有答案