PDA

View Full Version : expected type-specifier before 'Class Name' / not declared in this scope



sfabel
27th May 2010, 05:13
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.


#include <myClass.h>

int main(int argc, char *argv[])
{
QApplication a(argc, 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.



class MyClass LIBMYCLASS_EXPORT : public MyOtherClass


I get the following error when running "Build" in QtCreator:

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

tbscope
27th May 2010, 06:37
Did you include the header file that defines MyClass?

sfabel
27th May 2010, 12:12
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

tbscope
27th May 2010, 12:16
And the ManiDialog include?

sfabel
27th May 2010, 22:41
Yep, the include files are all there.

This compiles:



#include <QtGui/QApplication>

#include "manidialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ManiDialog *w = new ManiDialog( 0 );

w->show();
return a.exec();
}


This doesn't:



#include <QtGui/QApplication>

#include <myclass.h>

#include "manidialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

ManiDialog *w = new ManiDialog( 0 );

MyClass *m = new MyClass();

w->show();
return a.exec();
}


Thanks
Stephan

SixDegrees
27th May 2010, 22:50
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.

squidge
27th May 2010, 22:53
Then perhaps the error is in myclass.h, so post that file.

and what is 'LIBMYCLASS_EXPORT' ?

sfabel
27th May 2010, 23:03
Here goes:



#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:


#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

sfabel
27th May 2010, 23:06
...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:


#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

squidge
27th May 2010, 23:19
Your constructor seems to be missing a semicolon?

sfabel
27th May 2010, 23:29
Your constructor seems to be missing a semicolon?

Yeah, sorry my fault. It's there in the source.

ChrisW67
28th May 2010, 00:10
#include <QtGui/QApplication>
#include <myclass.h>
#include "manidialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, 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:
#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.

sfabel
28th May 2010, 01:07
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