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
luisdr22
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B
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.. 

 

Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

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
Please use plain text.
Contributor
LittleGruz
Posts: 12
Registered: ‎09-02-2013
My Device: Z10

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".

Please use plain text.
BlackBerry Development Advisor
jomurray
Posts: 133
Registered: ‎09-06-2010
My Device: Z30

Re: Creating a constructor

Minimally just:

 

add

 

#include <string>

 

and change the signature to:

 

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

 

 

---------------------------------------
Please refrain from posting new questions in solved threads.
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Found a bug? Report it here
Follow me on Twitter @jcmrim
Please use plain text.
Contributor
luisdr22
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B

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 :smileyhappy:, 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?

Please use plain text.
BlackBerry Development Advisor
jomurray
Posts: 133
Registered: ‎09-06-2010
My Device: Z30

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;

 

 

 

---------------------------------------
Please refrain from posting new questions in solved threads.
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Found a bug? Report it here
Follow me on Twitter @jcmrim
Please use plain text.
BlackBerry Development Advisor
jomurray
Posts: 133
Registered: ‎09-06-2010
My Device: Z30

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_ */

 

---------------------------------------
Please refrain from posting new questions in solved threads.
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Found a bug? Report it here
Follow me on Twitter @jcmrim
Please use plain text.
Contributor
luisdr22
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B

Re: Creating a constructor

many thanks!

Please use plain text.
Contributor
luisdr22
Posts: 20
Registered: ‎09-02-2013
My Device: Dev Alpha B

Re: Creating a constructor

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

Please use plain text.