Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. expected class-name error
Forum Update on Monday, May 27th 2025

expected class-name error

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 8.9k Views
  • 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.
  • N Offline
    N Offline
    Niagarer
    wrote on 7 Sept 2017, 10:36 last edited by Niagarer 9 Jul 2017, 10:37
    #1

    Hey,
    I have built this class:

    #ifndef FUNCTIONNODE_H
    #define FUNCTIONNODE_H
    
    #include "node.h"
    
    class FunctionNode : public Node
    {}; //error here
    #endif // FUNCTIONNODE_H
    

    And get this error:

    error: expected class-name before '{' token
     {
     ^
    

    And I don't know why. The class name is there, so the mistake is different to the error I think, but I got no other errors.
    Any ideas?
    Thanks for answers

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 7 Sept 2017, 10:38 last edited by
      #2

      are you sure it knows Node ?
      and its not node or anything like that ?

      N 1 Reply Last reply 7 Sept 2017, 10:41
      0
      • M mrjj
        7 Sept 2017, 10:38

        are you sure it knows Node ?
        and its not node or anything like that ?

        N Offline
        N Offline
        Niagarer
        wrote on 7 Sept 2017, 10:41 last edited by Niagarer 9 Jul 2017, 10:45
        #3

        I added

        class Node;
        

        and now I get this:

         error: invalid use of incomplete type 'class Node'
         class FunctionNode : public Node
                                     ^
        

        Looks like you are right, it didn't know class Node but I unfortunately don't know what this really means... why is it incomplete type? Class Node is a QGraphicsItem derived class.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 7 Sept 2017, 10:46 last edited by mrjj 9 Jul 2017, 10:48
          #4

          Hi

          this compiles fine here
          class TestNode{};
          class TestNodeChild : TestNode {
          };

          Incomplete means "Hey you only told me the class name"
          Nothing more.
          And that is not so good with inheritance as it needs to know base class ctor etc.

          When you do
          class node;
          That is a forward. And tells only name.
          It speed up compilation when allowed.
          The cure is full include
          #include "node.h"

          N 1 Reply Last reply 7 Sept 2017, 10:52
          2
          • M mrjj
            7 Sept 2017, 10:46

            Hi

            this compiles fine here
            class TestNode{};
            class TestNodeChild : TestNode {
            };

            Incomplete means "Hey you only told me the class name"
            Nothing more.
            And that is not so good with inheritance as it needs to know base class ctor etc.

            When you do
            class node;
            That is a forward. And tells only name.
            It speed up compilation when allowed.
            The cure is full include
            #include "node.h"

            N Offline
            N Offline
            Niagarer
            wrote on 7 Sept 2017, 10:52 last edited by
            #5

            @mrjj
            Ok... This is weird because I did include node.h . What does it need more?
            It has the class name and the header file. Is there a problem, that Node derives from QGraphicsItem or sth?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 7 Sept 2017, 11:51 last edited by
              #6

              Hi,

              Can you also post the content of node.h

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2
              • M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 7 Sept 2017, 12:02 last edited by
                #7

                @Niagarer said in expected class-name error:

                Is there a problem, that Node derives from QGraphicsItem or sth?

                Nope.
                Unless the .h have some error that prevents parsing, then it should be fine.

                But as mr sgaist says, please show whole file so we can see if we spot it.

                I assume its not just because in .h file node and you use Node. (big vs small letters)

                1 Reply Last reply
                1
                • N Offline
                  N Offline
                  Niagarer
                  wrote on 7 Sept 2017, 12:38 last edited by
                  #8

                  SOLUTION
                  Yes, the problem was a bit bigger.

                  Summarized:
                  It was a problem with including other classes in the header files of the classes. The solution is, to use forward declaration ( class myClass; ) and include the myclass.h in the source (.cpp) file, not in the header file. I did not do this consequent and so, my derived class knew the parent class but thought, it would not have any content. Why, is good explained here (look to the answer post by Ben Voigt):
                  https://stackoverflow.com/questions/5319906/error-expected-class-name-before-token

                  Thanks for your help!!

                  1 Reply Last reply
                  1

                  1/8

                  7 Sept 2017, 10:36

                  • Login

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