How do I forward declare a typedef in C++?

时间:2021-04-28 20:56:58

I have two namespaces (F and M) where I used typedef to define something. I use the typedef in one namespace to declare a variable in the other namespace.

我有两个命名空间(F和M),我使用typedef来定义一些东西。我在一个名称空间中使用typedef来声明另一个名称空间中的变量。

For example I have these files:

例如,我有这些文件:

File M.hpp

文件M.hpp

#ifndef M_HPP
#define M_HPP

#include "F.hpp"

namespace M{
    typedef std::vector<F::myD> VectorDouble;

class M{
    private:
        VectorDouble Diction;
};
}

#endif  // M_HPP

File F.hpp

文件F.hpp

#ifndef F_HPP
#define F_HPP

#include "M.hpp"

namespace F{
    typedef double myD;

class MyF{
    private:
        M::VectorDouble myVar;
};
}

#endif  // F_HPP

It is immediately clear that these two header files create a circular dependance so forward declaration may be necessary, but how to do that with namespaces and typedefs?

很明显,这两个头文件创建循环依赖,因此可能需要前向声明,但如何使用命名空间和typedef?

File namespace.cpp to drive the code:

文件namespace.cpp来驱动代码:

#include <iostream>
#include <vector>

#include "M.hpp"
#include "F.hpp"

int main(){
    std::cout << "Learning how to access stuff in a namespace." << std::endl;

    F::MyF myFInstance;
    M::M myMInstance;

    return 0;
}

When I try to compile, I get an error that my M is an undeclared identifier (see exact error message below). I don't understand why M isn't seen as a namespace.

当我尝试编译时,我收到一个错误,我的M是未声明的标识符(请参阅下面的确切错误消息)。我不明白为什么M不被视为名称空间。

$ clang++ -std=c++11 -stdlib=libc++ namespace.cpp -o namespace
In file included from namespace.cpp:5:
In file included from ./M.hpp:5:
./F.hpp:12:9: error: use of undeclared identifier 'M'
        M::VectorDouble myVar;
        ^
1 error generated.

How can I access a typedef from another namespace? Is this a forward declaration issue?

如何从其他命名空间访问typedef?这是一个前瞻性声明问题吗?

2 个解决方案

#1


1  

Your issue is that you have created circular includes.

您的问题是您已创建循环包含。

By your own coding, your file F.hpp can't be compiled without first including M.hpp.

通过自己的编码,如果没有先包含M.hpp,就无法编译文件F.hpp。

And M.hpp can't be compiled without first including F.hpp.

如果没有首先包含F.hpp,则无法编译M.hpp。

Therefore, neither header can be compiled. See this SO post for solutions to circular dependencies.

因此,既不能编译头文件。请参阅此SO帖子以获取循环依赖关系的解决方案。

Edit:

编辑:

You can forward declare your typedef like this.

你可以像这样转发声明你的typedef。

File F_fwd.hpp

文件F_fwd.hpp

#ifndef F_FWD_HPP
#define F_FWD_HPP

namespace F{
    typedef double myD;
}

#endif // F_FWD_HPP

#2


0  

your two headers are included in each other, that would lead to circular reference and with the header gaurds, one file can be completely excluded in one of the other headers.

你的两个标题相互包含,这将导致循环引用,并且标题gaurds,一个文件可以完全排除在其他标题之一。

#1


1  

Your issue is that you have created circular includes.

您的问题是您已创建循环包含。

By your own coding, your file F.hpp can't be compiled without first including M.hpp.

通过自己的编码,如果没有先包含M.hpp,就无法编译文件F.hpp。

And M.hpp can't be compiled without first including F.hpp.

如果没有首先包含F.hpp,则无法编译M.hpp。

Therefore, neither header can be compiled. See this SO post for solutions to circular dependencies.

因此,既不能编译头文件。请参阅此SO帖子以获取循环依赖关系的解决方案。

Edit:

编辑:

You can forward declare your typedef like this.

你可以像这样转发声明你的typedef。

File F_fwd.hpp

文件F_fwd.hpp

#ifndef F_FWD_HPP
#define F_FWD_HPP

namespace F{
    typedef double myD;
}

#endif // F_FWD_HPP

#2


0  

your two headers are included in each other, that would lead to circular reference and with the header gaurds, one file can be completely excluded in one of the other headers.

你的两个标题相互包含,这将导致循环引用,并且标题gaurds,一个文件可以完全排除在其他标题之一。