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
Developer
MikePembo951
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB
Accepted Solution

C++ compiler

Hi,

I'm having problems compiling my project. Every time I build (without changing anything) it returns a different number of errors. I think its a problem with my includes and classes. I'm using #ifndef for each class.

I have #includes in the source files and namespace std in header enclosed in #ifndef.

I have 6 header/classes (+main.cpp) so I should probably start drawing a flowchart/diagram for this :/

 

Example of error:

 

view.H - View's header

#ifndef view_h

#define view_h

//lots of definitions etc. and when GLfloat is used as a type i get this:

"'GLfloat' does not name a type" 

#endif

 

view.cpp - View's source

#include "view.h"

#include <GLES/gl.h>

 

Clearly gl.h is included in View's source and GL types have no problem being used in the source, it seems the header cannot access them. 

I should also note that these errors im getting are not consistent, I can build fine, then immediatly build again and it bring up errors.

 

I know I probably have some problems with my includes but is the compiler working correctly?

Why are the errors inconsistent? 

(Im not using SDK2.1 im using 2.0.0)

 

Please use plain text.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: C++ compiler

If you have too many files in the project then you might have 'inconsistent' errors. Fix them one by one and they all will go away.

If you want to use GLfloat type in view.h then you must include gl.h before view.h, try:

#include <GLES/gl.h>

#include "view.h"

Please use plain text.
Developer
MikePembo951
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB

Re: C++ compiler

Ok, so the order of my #includes matters?.

So in the source file of classes i should put all includes before the header file include.

Thanks,

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: C++ compiler

It's really quite straightforward.

The compiler only compiles your view.cpp file.  It doesn't ever compile .h files.

Whenever an #include is encountered, the compiler's preprocessor simply copy&pastes the entire contents of that included file into your source file.

So when the compiler encountered your view.cpp file, it effectively did the following:

-copy&paste the contents of view.h into view.cpp at the #include <view.h> line.

-copy&paste the contents of GLES/gl.h into view.cpp at the next line. (recursively including all other headers included by GLES/gl.h).

-Now the compiler is clearly confused because you are referencing a GLfloat prior to defining what a GLfloat is.  (GLfloat being defined as part of the GLES/gl.h includes).

 

It's as if you wrote code that did the following:

void brokenFunction() {
   myType x = 1;
typedef int myType;
}

You can't reference myType without defining it first.

 

There are two straightforward ways to resolve your problem.  One is to include GLES/gl.h first in your view.cpp file and then view.h second.  The other solution would be to include GLES/gl.h somewhere in your view.h file.

 

Cheers,

Sean

 

Please use plain text.
Developer
MikePembo951
Posts: 107
Registered: ‎02-15-2012
My Device: Blackberry Playbook 32GB

Re: C++ compiler

ah ok thanks :smileyhappy:

But just ran in to another problem,

I have a class in one source file say foo which has a member dependant on a class from bar.

So I #include bar.h in foo.h. (inside #ifndef).

 

Now I have another class in bar which is dependant on something from foo.

#So I #include foo.h in bar.h. (inside #ifndef).

 

It wont define anything twice since I have #ifndef protecting multiple definitions.

But now it wont compile. Its saying it is missing declarations made in the other file.

I'm a c++ beginner and this is probably a school boy error.

 

Could you point me in the right direction for #includes, i've heard of forward declaration but im not sure how its used or even whether it could help this.

Thanks,

Mike

Please use plain text.
Developer
BGmot
Posts: 1,068
Registered: ‎11-24-2011
My Device: PlayBook

Re: C++ compiler

It's indeed just C++ and not related to BlackBerry. You can google it but in two words: you can declare class and define it later.

so in your foo.h put

class bar;

somewhere before you define class foo. This way you can reference class bar without this class being defined (yet).

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 668
Registered: ‎11-29-2011
My Device: developer

Re: C++ compiler

Ah, that requires a forward declaration.

Typically you do something like:

 

class bar;

class foo {
  foo(bar* theBar);   // this is OK
  bar theBar;         // this is NOT OK 
};

class bar {
  bar(foo* theFoo);
foo theFoo; // this is OK, since class foo is not incomplete.
};

 

The forward declaration on the first line lets the compiler know that a class called bar exists.  Until it is defined, bar exists as what is called an "incomplete type".

Incomplete types can only be referenced by pointer, since their storage size is not known.  This is why the "this is OK" line works, and the "this is NOT OK" line does not work.  Note that in class bar, it is permissable to declare theFoo, because foo is not incomplete (unlike bar).  This sort of thing is also very common when defining a struct which implements a linked list.

 

Can you please try out a google search first on things like this?  This is all pretty basic C/C++ stuff that is covered in great detail in numerous beginner tutorials and examples.

 

Cheers,

Sean

Please use plain text.