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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Contributor
Posts: 11
Registered: ‎10-28-2012
My Device: Z10
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

Developer
Posts: 271
Registered: ‎05-29-2013
My Device: BlackBerry Z10
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
Highlighted
Contributor
Posts: 11
Registered: ‎10-28-2012
My Device: Z10
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

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

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
Contributor
Posts: 11
Registered: ‎10-28-2012
My Device: Z10
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... Smiley Happy

 

Thnx again