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?
These two classes are two different classes (with same name).
You're using
classspace::Aclass
which has not defined yet. Add the body in the namespce:This declares a class inside a namespace.
This declares and defines a different class with the same name in the global namespace
This defines the class you previously declared in the namespace.
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
, andMainWindow
in the global namespace. The one inUi
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 beint
.You declare the class within the namespace but define it outside the namespace. So the declaration is:
but the definition defines the implementation for:
Namespaces should match for declaration and definition.
Imagine
Which one are we using?
foo
orgoo
?You must specify, either by calling all uses of AClass with the namespace name, i.e
foo::AClass
, or by placing all uses of it insideNamespaces are essentially packages. In your Qt example, the class is forward declared. One way to use namespaces is as follows:
If you want to do it like your first example, then you must forward declare it. For example,
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 :
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.