way of defining class in a namespace

2019-01-28 10:14发布

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?

6条回答
小情绪 Triste *
2楼-- · 2019-01-28 10:55
namespace classspace
{
    class Aclass;
}

class Aclass
{
    //body
};

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:

namespace classspace
{

class Aclass
{
    //body
};

}
查看更多
对你真心纯属浪费
3楼-- · 2019-01-28 10:56
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.

查看更多
该账号已被封号
4楼-- · 2019-01-28 10:56

You declare the class within the namespace but define it outside the namespace. So the declaration is:

classspace::Aclass

but the definition defines the implementation for:

::Aclass

Namespaces should match for declaration and definition.

查看更多
We Are One
5楼-- · 2019-01-28 10:58

Imagine

namespace foo
{
  class AClass;
}
namespace goo
{
  class AClass;
}

class AClass
{
}

int main()
{
  AClass myClass;
}

Which one are we using? foo or goo?

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 inside

namespace foo
{

}
查看更多
等我变得足够好
6楼-- · 2019-01-28 11:00

Namespaces are essentially packages. In your Qt example, the class is forward declared. One way to use namespaces is as follows:

namespace MyNamespace
{
  class MyClass
  {
  };
}

If you want to do it like your first example, then you must forward declare it. For example,

namespace MyNamespace
{
   class MyClass;
}

class MyClass
{
    //body
};
查看更多
迷人小祖宗
7楼-- · 2019-01-28 11:02

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.

查看更多
登录 后发表回答