Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Contributor
tipul07
Posts: 11
Registered: ‎10-28-2012
My Carrier: N/A
Accepted Solution

Accessing static variable...

Hello,

 

I need a static variable which holds a QMap, for this example I will use QMap<QString,QString>.

 

I created fresh new project and I changed in applicationui.cpp:

 

 

...
ApplicationUI::ApplicationUI(bb::cascades::Application *app) : QObject(app) { .... initDatabase( 1 ); initDatabase( 2 ); initDatabase( 3 ); initDatabase( 4 ); } bool ApplicationUI::initDatabase( int index ) { QString database = QString( "Db%1" ).arg( index ); QString value = QString( "data/db%1.sqlite" ).arg( index ); ApplicationUI::m_databases[database] = value; // undefined reference to `ApplicationUI::m_databases' return true; } ApplicationUI::~ApplicationUI() { if( ApplicationUI::m_databases.count() ) // undefined reference to `ApplicationUI::m_databases' { foreach( const QString &key, ApplicationUI::m_databases.keys() ) // undefined reference to `ApplicationUI::m_databases' { QString db = ApplicationUI::m_databases[key]; // undefined reference to `ApplicationUI::m_databases' qDebug() << "Closing [" << db << "]"; } } }

 and in applicationui.hpp:

 

...

class ApplicationUI : public QObject
{
    Q_OBJECT
public:
    ApplicationUI(bb::cascades::Application *app);
    ~ApplicationUI();

    static QMap<QString,QString> m_databases;
    bool initDatabase( int index );
...
};

#endif /* ApplicationUI_HPP_ */

 

As you can see commented in the code above I get as error:
undefined reference to `ApplicationUI::m_databases' applicationui.cpp

 

I tought accessing static variables like ApplicationUI::m_databases would work.


Can you please guide me here.

 

Regards,

Andy

Please use plain text.
Developer
saprykin
Posts: 271
Registered: ‎05-29-2013
My Carrier: MTS

Re: Accessing static variable...

Sometimes I'm receiving such an error after adding methods to the class and the problem can be resolved with rebuilding IDE's C++ indexer (right click on the project name - Index - Rebuild). Try it.

-------------------------------------------------------------------------------
NSR Reader for BlackBerry 10 @NSRReader on Twitter
Me @xelfium on Twitter
Please use plain text.
Contributor
tipul07
Posts: 11
Registered: ‎10-28-2012
My Carrier: N/A

Re: Accessing static variable...

Hi saprykin,

 

I tried rebuilding index, but still no joy. I noticed that after a run/build command IDE doesn't refresh code indexes and errors as you change code, so I always have to run/build to be sure errors displayed are from new/current code.

 

Anyway this error is pretty frustrating as it is a basic static variable access issue. Short story:

 

class MyClass
{
public:
      MyClass();
      ~MyClass();

      void someMethod();

      static QMap<QString,QString> variable;
};

void MyClass::~MyClass()
{
     // do some cleanup... using MyClass::variable, doesn't work
}

void MyClass::someMethod()
{
     MyClass::variable["one"] = "value One"; // doesn't work...
}

 This code was not compiled, take it as pseudo-code to better understand my issue.

 

Regards,

Andy

Please use plain text.
Developer
Zmey
Posts: 1,494
Registered: ‎12-18-2012

Re: Accessing static variable...

[ Edited ]

Hi! This is what I spotted:

 

1) Is #include <QMap> present in header file?

 

2) 'void' is syntax error:

void MyClass::~MyClass()

3) 

static QMap<QString,QString> variable;

is a declaration, you'll also need to create the variable in .cpp file. Add to .cpp:

QMap<QString, QString> MyClass::variable;

 4) 

MyClass::variable["one"] = "value One"; // doesn't work...

 "MyClass::" is not needed. variable["one"] should work too.

 

 

 

 UPD: this compiles (I replaced QMap with std::map and QString with std::string for quick test):

 

#include <map>
#include <string>

class MyClass
{
public:
      MyClass();
      ~MyClass();

      void someMethod();

      static std::map<std::string,std::string> variable;
};

std::map<std::string, std::string> MyClass::variable;

MyClass::~MyClass()
{
     // do some cleanup... using MyClass::variable, doesn't work
}

void MyClass::someMethod()
{
     variable["one"] = "value One"; // doesn't work...
}

int main(void)
{
        return 0;
}

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Contributor
tipul07
Posts: 11
Registered: ‎10-28-2012
My Carrier: N/A

Re: Accessing static variable...

Thnx Zmey!

 

The issue was I was not creating variable in cpp file. So what I needed was:

 

QMap<QString, QString> MyClass::variable;

 

Memo to myself on static variables... :smileyhappy:

 

Thnx again

Please use plain text.