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.
Code:
#include <myClass.h>
int main(int argc, char *argv[])
{
ManiDialog *w = new ManiDialog( 0 );
MyClass *m = new MyClass();
w->show();
return a.exec();
}
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.
Code:
class MyClass LIBMYCLASS_EXPORT : public MyOtherClass
I get the following error when running "Build" in QtCreator:
Code:
main.cpp:10: error: ‘m’ was not declared in this scope
main.cpp:10: error: expected type-specifier before ‘MyClass’
main.cpp:10: error: expected ‘;’ before ‘MyClass’
make: *** [main.o] Error 1
Can anyone help me out? This seems so straightforward...
Thanks,
Stephan
Re: expected type-specifier before 'Class Name' / not declared in this scope
Did you include the header file that defines MyClass?
Re: expected type-specifier before 'Class Name' / not declared in this scope
Quote:
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
Re: expected type-specifier before 'Class Name' / not declared in this scope
And the ManiDialog include?
Re: expected type-specifier before 'Class Name' / not declared in this scope
Yep, the include files are all there.
This compiles:
Code:
#include <QtGui/QApplication>
#include "manidialog.h"
int main(int argc, char *argv[])
{
ManiDialog *w = new ManiDialog( 0 );
w->show();
return a.exec();
}
This doesn't:
Code:
#include <QtGui/QApplication>
#include <myclass.h>
#include "manidialog.h"
int main(int argc, char *argv[])
{
ManiDialog *w = new ManiDialog( 0 );
MyClass *m = new MyClass();
w->show();
return a.exec();
}
Thanks
Stephan
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.
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' ?
Re: expected type-specifier before 'Class Name' / not declared in this scope
Here goes:
Code:
#ifndef MYCLASS_H
#define MYCLASS_H
#include "libmyclass_global.h"
#include "myotherclass.h"
class LIBMYCLASS_EXPORT MyClass : public MyOtherClass
{
Q_OBJECT
public:
MyClass()
virtual ~MyClass();
virtual void initialize();
virtual void printDebug() const;
public slots:
virtual void control();
};
#endif // MYCLASS_H
MyOtherClass.h:
Code:
#ifndef MYOTHERCLASS_H
#define MYOTHERCLASS_H
#include <QObject>
#include <QString>
#include <QTimer>
#include "myclass_global.h"
class LIBMYCLASS_EXPORT MyOtherClass
: public QObject{
Q_OBJECT
public:
MyOtherClass( );
virtual ~MyOtherClass();
virtual void initialize();
virtual void printDebug() const;
public slots:
virtual void control() = 0;
};
#endif // MYOTHERCLASS_H
Re: expected type-specifier before 'Class Name' / not declared in this scope
Quote:
Originally Posted by
fatjuicymole
...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:
Code:
#ifndef LIBMYCLASS_GLOBAL_H
#define LIBMYCLASS_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(LIBMYCLASS_LIBRARY)
# define LIBMYCLASS_EXPORT Q_DECL_EXPORT
#else
# define LIBMYCLASS_EXPORT Q_DECL_IMPORT
#endif
#endif // LIBMYCLASS_GLOBAL_H
Re: expected type-specifier before 'Class Name' / not declared in this scope
Your constructor seems to be missing a semicolon?
Re: expected type-specifier before 'Class Name' / not declared in this scope
Quote:
Originally Posted by
fatjuicymole
Your constructor seems to be missing a semicolon?
Yeah, sorry my fault. It's there in the source.
Re: expected type-specifier before 'Class Name' / not declared in this scope
Code:
#include <QtGui/QApplication>
#include <myclass.h>
#include "manidialog.h"
int main(int argc, char *argv[])
{
ManiDialog *w = new ManiDialog( 0 );
MyClass *m = new MyClass();
w->show();
return a.exec();
}
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:
Code:
#include "myclass.h"
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.
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