Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Problem with forward declaration
Forum Updated to NodeBB v4.3 + New Features

Problem with forward declaration

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 4 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

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

    @

    • src
      role.h
      role.cpp
      • daemon
        daemon.h
        daemon.cpp
        @

    where daemon.h uses Role via forward declaration:

    @
    // daemon.h

    // forward declaration of the Role implementation
    class Role;

    class Daemon : public QObject{
    ...

    QList<Role> roles();
    }
    @

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

    @error: ‘Role’ was not declared in this scope@

    Am I missing something?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MaQzma
      wrote on last edited by
      #2

      Must be a pointer, like this:
      @
      QList<Role *> roles();
      @

      In daemon.cpp
      @
      #include "role.h"
      @

      Mario.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #3

        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.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved