June 14, 2012

fluca1978 fluca1978
Ant Farmer
524 posts

problem with forward declaration

 

Hi all,
I’ve got a problem with a forward declaration. My source tree structure is as follows:

  1. + src
  2.     role.h
  3.     role.cpp
  4.     + daemon
  5.         daemon.h
  6.         daemon.cpp

where daemon.h uses Role via forward declaration:

  1. // daemon.h
  2.  
  3. // forward declaration of the Role implementation
  4. class Role;
  5.  
  6. class Daemon : public QObject{
  7.    ...
  8.  
  9.    QList<Role> roles();
  10. }

and when I compile the project I got the following error for the Daemon::roles() method:

  1. error: ‘Role’ was not declared in this scope

Am I missing something?

4 replies

June 14, 2012

Mariø™ Mariø™
Hobby Entomologist
43 posts

Must be a pointer, like this:

  1. QList<Role *> roles();

In daemon.cpp

  1. #include "role.h"

 Signature 

To be something. You need to do something.

June 14, 2012

mlong mlong
Mad Scientist
1517 posts

Your QList is of Role objects, not of Role pointers. A forward declaration would be sufficient for pointers, but since you are using the entire object, you’ll need to #include “role.h” in your daemon.h file.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

June 15, 2012

fluca1978 fluca1978
Ant Farmer
524 posts

Thanks, I was not aware forward declaration worked only for pointers, but it makes sense!

June 22, 2012

Volker Volker
Robot Herder
5428 posts

It works for pointers and references (which technically are more or less pointers) and, in both cases, only as long as you need the pointer or reference, but not any of the methods or members of the class.

 
  ‹‹ Override "operator <<" for char and wchar_t combined      does constant improve performances? ››

You must log in to post a reply. Not a member yet? Register here!