简体   繁体   English

是否可以转发声明名称空间中的typedef?

[英]Is it possible to forward declare a typedef that is within a namespace?

I've looked around and I can't quite tell if the other similar questions answer this or not. 我环顾四周,我无法完全确定其他类似问题是否回答了这个问题。

// lib.h
namespace lib_namespace
{
  struct lib_struct
  {
    typedef std::vector<LibObject> struct_value;
  };

  typedef lib_struct::struct_value lib_value; // compiler points here
};

// my.h
// attempt at forward declaration
namespace lib_namespace { class lib_value; };
...

// my.cpp
#include "lib.h"

I get a redefinition compiler error which is understandable, but is there a way to forward declare that typedef? 我收到一个可以重新定义的重新定义编译器错误,但是有没有办法向前声明那个typedef?

My intent is to avoid adding lib.h as a dependency outside of the library I'm making. 我的目的是避免将lib.h作为依赖项添加到我正在创建的库之外。 Perhaps there's a better way to achieve that? 也许有更好的方法可以做到这一点?

Edit: To clarify, I'm trying to avoid adding an additional include directories line to all the project files that would use the library I'm creating because of the third party library I'm using and the above situation is where I'm stuck. 编辑:为澄清起见,我正在尝试避免在所有将使用我正在创建的库的项目文件中添加额外的包含目录行,因为我正在使用第三方库,而以上情况是我正在使用的卡住。 So it's OK if I include lib.h in my.cpp , but not my.h . 所以没关系,如果我有lib.hmy.cpp ,但不my.h

Namespaces are not relevant to the question. 命名空间与问题无关。 You cannot forward declare a typedef; 您不能转发声明typedef。 only actual types (classes and structs) can be forward declared. 只能实际声明实际类型(类和结构)。

You compiler complains, because lib.h and my.h are independent header files and they don't know about each other. 您的编译器会抱怨,因为lib.hmy.h是独立的头文件,而且彼此之间并不了解。 Moreover, unlike the actual class , the typedef doesn't support the forward declaration. 而且,与实际的class不同, typedef不支持正向声明。

Suppose, you don't want to #include"lib.h" , then you can create a separate header which has typedef lib_struct::struct_value lib_value; 假设您不想#include"lib.h" ,则可以创建一个单独的标头,其类型为typedef lib_struct::struct_value lib_value; and the relevant part related to it. 以及与此相关的部分。 #include that new header wherever needed. #include在需要的地方包含新标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM