Results 1 to 13 of 13

Thread: expected type-specifier before 'Class Name' / not declared in this scope

  1. #1
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question expected type-specifier before 'Class Name' / not declared in this scope

    Hi ppl,

    have a relatively straightforward problem, but can't seem to figure out what's happening here.

    I've created a shared library with Qt. It compiles fine, and is installed. I try to instantiate an object from one of the defined classes in main.cpp of a program using the library.

    Qt Code:
    1. #include <myClass.h>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. ManiDialog *w = new ManiDialog( 0 );
    8.  
    9. MyClass *m = new MyClass();
    10.  
    11. w->show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    The library is added to the .pro file, the include path is set. I've used the Q_DECL_EXPORT macro in the library (QtCreator created this automatically for me). I don't know if that is important to this. MyClass is a concrete class, whose abstract base class is also defined in the library.

    Qt Code:
    1. class MyClass LIBMYCLASS_EXPORT : public MyOtherClass
    To copy to clipboard, switch view to plain text mode 

    I get the following error when running "Build" in QtCreator:
    Qt Code:
    1. main.cpp:10: error: ‘m’ was not declared in this scope
    2. main.cpp:10: error: expected type-specifier before ‘MyClass’
    3. main.cpp:10: error: expected ‘;’ before ‘MyClass’
    4. make: *** [main.o] Error 1
    To copy to clipboard, switch view to plain text mode 

    Can anyone help me out? This seems so straightforward...

    Thanks,
    Stephan

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Did you include the header file that defines MyClass?

  3. #3
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Did you include the header file that defines MyClass?
    Yes. Unfortunately, I can't think of anything else that would cause this error. Interestingly enough, if I leave out the header file include, I get an additional error (can't remember right now, but something along the lines of 'undefined type 'MyClass'' or something similar).

    Thanks for any help.

    Stephan

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    And the ManiDialog include?

  5. #5
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Yep, the include files are all there.

    This compiles:

    Qt Code:
    1. #include <QtGui/QApplication>
    2.  
    3. #include "manidialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. ManiDialog *w = new ManiDialog( 0 );
    10.  
    11. w->show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 

    This doesn't:

    Qt Code:
    1. #include <QtGui/QApplication>
    2.  
    3. #include <myclass.h>
    4.  
    5. #include "manidialog.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. ManiDialog *w = new ManiDialog( 0 );
    12.  
    13. MyClass *m = new MyClass();
    14.  
    15. w->show();
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Stephan

  6. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    I'll note that your include file is called "myClass.h" (or "myclass.h" in your second example) while the class itself is "MyClass"; the difference in case shouldn't make any difference to the compiler, but some of the Qt tools can be fussy about such things, so it's best to be consistent.

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Then perhaps the error is in myclass.h, so post that file.

    and what is 'LIBMYCLASS_EXPORT' ?

  8. #8
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Here goes:

    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4.  
    5. #include "libmyclass_global.h"
    6. #include "myotherclass.h"
    7.  
    8.  
    9. class LIBMYCLASS_EXPORT MyClass : public MyOtherClass
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyClass()
    15. virtual ~MyClass();
    16.  
    17. virtual void initialize();
    18. virtual void printDebug() const;
    19.  
    20. public slots:
    21. virtual void control();
    22. };
    23.  
    24. #endif // MYCLASS_H
    To copy to clipboard, switch view to plain text mode 

    MyOtherClass.h:
    Qt Code:
    1. #ifndef MYOTHERCLASS_H
    2. #define MYOTHERCLASS_H
    3.  
    4. #include <QObject>
    5. #include <QString>
    6. #include <QTimer>
    7.  
    8. #include "myclass_global.h"
    9.  
    10. class LIBMYCLASS_EXPORT MyOtherClass : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. MyOtherClass( );
    16. virtual ~MyOtherClass();
    17.  
    18. virtual void initialize();
    19. virtual void printDebug() const;
    20.  
    21. public slots:
    22. virtual void control() = 0;
    23. };
    24.  
    25. #endif // MYOTHERCLASS_H
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Quote Originally Posted by fatjuicymole View Post
    ...and what is 'LIBMYCLASS_EXPORT' ?
    Sorry - I don't know about that. I am using Qt Creator and started a project "shared library" when it added this, with the global include file like so:
    Qt Code:
    1. #ifndef LIBMYCLASS_GLOBAL_H
    2. #define LIBMYCLASS_GLOBAL_H
    3.  
    4. #include <QtCore/qglobal.h>
    5.  
    6. #if defined(LIBMYCLASS_LIBRARY)
    7. # define LIBMYCLASS_EXPORT Q_DECL_EXPORT
    8. #else
    9. # define LIBMYCLASS_EXPORT Q_DECL_IMPORT
    10. #endif
    11.  
    12. #endif // LIBMYCLASS_GLOBAL_H
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Your constructor seems to be missing a semicolon?

  11. #11
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Quote Originally Posted by fatjuicymole View Post
    Your constructor seems to be missing a semicolon?
    Yeah, sorry my fault. It's there in the source.

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <myclass.h>
    3. #include "manidialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. ManiDialog *w = new ManiDialog( 0 );
    9. MyClass *m = new MyClass();
    10. w->show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    At line 2 you are asking the pre-processor to look for the file myclass.h in the system include directory path. This path is a compound of a built-in value and any "-I" directives (or equivalents) and typically does not include the current directory. You should be receiving a pre-processor error saying it cannot find myclass.h before you get the error message you are describing here.

    Have you tried:
    Qt Code:
    1. #include "myclass.h"
    To copy to clipboard, switch view to plain text mode 
    The compiler will look in all the same places for the file but it will look in the current (actually same directory as the cpp file) directory first.
    Last edited by ChrisW67; 27th May 2010 at 23:11. Reason: spelling corrections

  13. #13
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: expected type-specifier before 'Class Name' / not declared in this scope

    Thank you guys for your help. In case you haven't noticed, I tried to clean up the code before posting to remove any unnecessary clutter.

    However, it seems that by doing that, the error actually went away. So I will keep re-building the MyClass and MyOtherClass until the error starts to appear again. As far as I can tell, there's nothing wrong with the code I've posted so far.

    (The includes are all found, per compiler directive -I/usr/local/include).

    I'll get to the bottom of this, and write my findings here, so in case this happens again I don't waste another day or two finding the error.

    Thanks,
    Stephan

Similar Threads

  1. `lineEdit' was not declared in this scope?
    By i4ba1 in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2009, 14:36
  2. QDomDocument was not declared in this scope
    By grantbj74 in forum Newbie
    Replies: 5
    Last Post: 25th August 2009, 09:43
  3. glutSolidSphere was not declared in this scope error
    By nuts_fever_007 in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 04:56
  4. Replies: 2
    Last Post: 28th December 2007, 18:30
  5. error: 'connect' was not declared in this scope ??
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 15:27

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.