I defined a class in a namespace in a header as follows
#ifndef _c1_
#define _c1_
namespace classspace
{
class Aclass;
}
class Aclass
{
//body
};
#endif _c1_
I added this header to main.cpp and made an object in main() but its returning error that undefined class 'classspace::Aclass'
its my main
void main()
{
classspace::Aclass b;
}
when I defined class as
class classspace::Aclass
{
//body
};
error resolved. I saw in Qt mainwindow file using first approach:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
is working without any error. what is my mistake in the first approach?
The class definition must be in the same namespace you declared the class in.
As for the Qt example, the MainWindow declared outside of the namespace isn't the same class.
It uses the Pimpl idiom. The MainWindow class declared in the namespace is used as a member in the MainWindow class declared outside, and is qualified with its namespace :
Ui::MainWindow* ui;
The definition of this class is put somewhere else (in a different .cpp file) where it should be in the Ui
namespace, or with definitions prefixed with the namespace.
namespace classspace
{
class Aclass;
}
This declares a class inside a namespace.
class Aclass
{
//body
};
This declares and defines a different class with the same name in the global namespace
class classspace::Aclass
{
//body
};
This defines the class you previously declared in the namespace.
void main()
{
classspace::Aclass b;
}
This tries to instantiate the class declared in the namespace. If that class hasn't been defined (only declared), then it is incomplete and can't be instantiated.
The Qt example involves two classes: Ui::MainWindow
, and MainWindow
in the global namespace. The one in Ui
has only been declared, so is incomplete in the header. You can do various things with it, such as declare a pointer to it, but you can't instantiate it.
Presumably, there is a separate source file which defines the Ui
class, instantiates one, and sets the pointer to point at it.
By the way, you shouldn't use reserved names for your include guards, or for anything else. Also, the return type of main
must be int
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With