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
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B
My Carrier: Claro Codetel
Accepted Solution

Creating a constructor

Hi, this is my first time posting here and my first time developing in Native Cascades.

 

im trying to do multi constructors class in c++, and im getting an error:

 

#include <bb/cascades/Image>
#include <bb/cascades/Label>
#ifndef INFORMATION_H_
#define INFORMATION_H_
using namespace bb::cascades;
class Information {
public:
	Information();
	Information(string name,string details,string image);
	~Information();
	Label* name;
	Label* details;
	Image* image;
};

#endif /* INFORMATION_H_ */

 but im getting an issue, it says this: expected ')' before 'name'

 

hope anybody know how to fix this.. 

 

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

Re: Creating a constructor

Hi,

 

Welcome to the forums.

 

string is std C++ class, but I don't see the corresponding includes. Probably it's better to use Qt's QString which is also used in other Cascades classes:

 

 

Information(const QString &name, ...etc)

 

Also, using 'using namespace' is not recommended in header files, this will lead to namespace pollution. It's better to use fully-qualified names in headers: bb::cascades::Label *name  etc.

 

Instead of using includes, classes can be forward-declared:

namespace bb { namespace cascades { class Label; } }

This will make the compilation faster. 

 

As you plan to use Cascades classes (Labels and Images), it's better to inherit the class from QObject adding proper includes and macros to class declaration and set the class as their parent.

 


Andrey Fidrya, @zmeyc on twitter
Contributor
Posts: 12
Registered: ‎09-02-2013
My Device: Z10
My Carrier: Telstra

Re: Creating a constructor

I agree with Zmey, using Qt's QString is probably better due to it being used by Cascades for text and that it also hosts some handy methods which std::string does not have.

 

I do wonder if the reason for the error is that you have not included an includes for the C++ string (#include <string>) and then declared any such strings with std::string since you have not added "using namespace std".

Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: Creating a constructor

Minimally just:

 

add

 

#include <string>

 

and change the signature to:

 

Information(std::string name, std::string details, std::string image);

 

 

Contributor
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B
My Carrier: Claro Codetel

Re: Creating a constructor

Many thanks, but i did that cause it understood string without including it.. my mistake, i changed to qstring and now its working Smiley Happy, btw im using the namespace and its not working...

 

/*
 * Information.h
 *
 *  Created on: Sep 2, 2013
 *      Author: luisdaniel
 */
#include <bb/cascades/Image>
#include <bb/cascades/Label>
#ifndef INFORMATION_H_
#define INFORMATION_H_
//using namespace bb::cascades;
namespace bb
{
	namespace cascades
	{
	class Label;
	class Image;
	}

}

class Information {
public:
	Information();
	Information(QString name,QString details,QString image);
	~Information();
	bb::cascades::Label* name;
	bb::cascades::Label* details;
	bb::cascades::Image* image;
};

#endif /* INFORMATION_H_ */

 in one example i saw this:

using namespace bb::cascades;

namespace bb
{
    namespace cascades
    {
        class Container;
        class ListView;
        class NavigationPane;
        class Page;
    }
}

 should i use both?

Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: Creating a constructor

The construct:

 

namespace bb
{
	namespace cascades
	{
	class Label;
	class Image;
	}

}

 is really just a way to forward declare the classes Label and Images it's a way of giving the compiler less work to do in that it doesn't need to #include and resolve all the nested include files. If you've already used:

 

#include <bb/cascades/Image>
#include <bb/cascades/Label>

 it's redundant. 

 

using namespace bb::cascades;

 is an instruction to the compiler to check for class definitions in this namespace if it can't find them elsewhere - adding this line will allow you to remove the namespace prefixes from:

 

	bb::cascades::Label* name;
	bb::cascades::Label* details;
	bb::cascades::Image* image;

 

 

 

Retired
Posts: 170
Registered: ‎09-06-2010
My Device: None
My Carrier: None

Re: Creating a constructor

Just to be clarity -- this works:

 

#include <bb/cascades/Image>
#include <bb/cascades/Label>
#ifndef INFORMATION_H_
#define INFORMATION_H_
using namespace bb::cascades;

class Information {
public:
	Information();
	Information(QString name,QString details,QString image);
	~Information();
	Label* name;
	Label* details;
	Image* image;
};

#endif /* INFORMATION_H_ */

 

Contributor
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B
My Carrier: Claro Codetel

Re: Creating a constructor

many thanks!

Contributor
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B
My Carrier: Claro Codetel

Re: Creating a constructor

i know, but i was trying to use it correctly.