+ Reply to Thread
Results 1 to 5 of 5

Thread: declaring subclasses in C++

  1. #1
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default declaring subclasses in C++

    hello, i'm trying to create subclasses to a class, but i get the same error to all my subclasses: expected class-name before*'{' token. Ive checked on other threads and site since it seems that this problem is very common. here is my code:

    MoyenDeTransport.h:
    Qt Code:
    1. class MoyenDeTransport
    2. {
    3. double litre;
    4. public:
    5. MoyenDeTransport(double);
    6. double getLitre();
    7. //void parcourir(int distance);
    8.  
    9. };
    To copy to clipboard, switch view to plain text mode 

    and here is my subclass:
    Camion

    Qt Code:
    1. class Camion: public MoyenDeTransport
    2. {
    3. public:
    4. Camion();
    5. };
    To copy to clipboard, switch view to plain text mode 

    as a matter of fact, i get one warning and one error:

    warning: In File included from camion.cpp:1,
    error: expected class-name before*'{' token

    here is my camion.cpp:
    Qt Code:
    1. #include "camion.h"
    2.  
    3. Camion::Camion()
    4. {
    5. }
    To copy to clipboard, switch view to plain text mode 

    and just in case, here is my MoyenDeTransport.cpp:

    Qt Code:
    1. #include "moyendetransport.h"
    2.  
    3. MoyenDeTransport::MoyenDeTransport(double ammount)
    4. {
    5. litre = ammount;
    6. }
    To copy to clipboard, switch view to plain text mode 


    I dont think i'm doing anything flagrantly wrong, but i'm sure its one small thing that i'm missing...

    What do you guys think?

    Paat

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    268
    Thanks
    21
    Thanked 28 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: declaring subclasses in C++

    I think you need to put a

    Qt Code:
    1. #include "MoyenDeTransport.h"
    To copy to clipboard, switch view to plain text mode 

    in your moyen.h to let the derived class know here the base class is

  3. #3
    Join Date
    Oct 2009
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declaring subclasses in C++

    i tried that and i get an infinite amount of errors, because by adding that line of code, you're refering moyen.cpp to moyen.h, which is linked to moyen.cpp, which includes moyen.h, which is linked to moyen.cpp, which.... its an infinite loop.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 345 Times in 332 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: declaring subclasses in C++

    why would a header file include moyen.cpp?

    Your camion class inherits MoyenDeTransport class, so Camion.h needs #include of MoyenDeTransport.h. Theres no need for MoyenDeTransport.h to include camion.h or any cpp files.

    Also, you should really guard your header files from multiple inclusion. It makes things a lot easier later on when multiple objects depend on the same base classes.

  5. #5
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: declaring subclasses in C++

    If your include directives leads to mutliple inclusions to the same file,
    you need to prevent from mutliple class definition with #ifndef ID with a unique id you define yourself

    So insert in your MoyenDeTransport.h :
    Qt Code:
    1. #ifndef MOYENDETRANSPORT_H
    2. #define MOYENDETRANSPORT_H
    3.  
    4. //definition of clas MoyenDeTransport
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 
    and in your Camion.h :
    Qt Code:
    1. #ifndef CAMION_H // or any unique id you define yourself
    2. #define CAMION_H
    3.  
    4. //defintion of class Camion
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 

+ Reply to Thread

Similar Threads

  1. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  2. problem in declaring QDirModel
    By prasanth.nvs in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 11th December 2008, 16:45
  3. Error declaring Qt object
    By Doug Broadwell in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2007, 21:15
  4. problem declaring a class
    By mickey in forum General Programming
    Replies: 1
    Last Post: 26th May 2006, 18:12
  5. declaring a friend
    By jayw710 in forum Qt Tools
    Replies: 2
    Last Post: 18th April 2006, 04:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts