简体   繁体   English

包含在头文件中

[英]Include in header files

class A{
private:
     std::string id;
public:
     void f();
};

gives compile time error. 给出编译时错误。 However, if I include <string> at top, it compiles correctly. 但是,如果我在顶部include <string> ,它会正确编译。 I don't want to use include statements in headers,though. 但是,我不想在标题中使用include语句。 How can i do it? 我该怎么做?

Including headers in other headers is a completely necessary thing. 在其他标题中包含标题是完全必要的。 It's wise to reduce it as much as possible, but fundamentally, if your class depends on std::string , then you have no choice but to #include <string> in the header. 尽可能减少它是明智的,但从根本上说,如果你的类依赖于std::string ,那么你别无选择,只能在头文件中使用#include <string> In addition, there's absolutely nothing wrong with depending on any and/or all Standard classes- they are, after all, mandated to be provided on any implementation. 此外,根据任何和/或所有标准类,绝对没有任何问题 - 毕竟,它们被强制要求在任何实现上提供。 It's using namespace std; using namespace std; that's frowned upon. 那令人沮丧。

You must include <string> in this case to be able to use std::string . 在这种情况下,您必须包含<string>才能使用std::string

The only moment when you can avoid #including a header is when you're only using references or pointers of the object in your header. 唯一可以避免#including标题的时刻是你只使用标题中对象的引用或指针。 In this case you can use forward declaration. 在这种情况下,您可以使用前向声明。 But since std::string is a typedef, you can't forward declare it , you have to include it. 由于std :: string是一个typedef,你不能转发声明它 ,你必须包含它。

I'm sure you're trying to follow the advice to try to #include as less as possible, but you can't follow it in this case. 我确定你试图按照建议尝试#include尽可能少,但在这种情况下你不能遵循它。

std::string is defined in the <string> header file in the std namespace. std :: string在std命名空间的<string>头文件中定义。 You have to include it. 你必须包括它。

You can make sure all dependencies are met by including the necessary files in the implementation files before including your other header files, ie make sure #include <string> appears on the first line in your implementation file (.cpp) before including your own header file. 您可以通过在包含其他头文件之前在实现文件中包含必要的文件来确保满足所有依赖项,即确保#include <string>出现在实现文件(.cpp)的第一行,然后再包含您自己的头文件文件。

This is not exactly best practice. 这不是最好的做法。 All header files should fulfill their own dependencies, so users of the header file do not need to care about dependencies. 所有头文件都应该满足自己的依赖关系,因此头文件的用户不需要关心依赖关系。 At least that is my humble opinion. 至少那是我的拙见。

You may have heard that it's unwise to use using namespace std; 你可能听说过using namespace std;是不明智的using namespace std; in a header, which is true because anything including that header is stuck with all of that in the global namespace. 在标题中,这是真的,因为包含该标题的任何内容都会被全局命名空间中的所有内容所困。 Including the header file that you need is perfectly acceptable. 包括您需要的头文件是完全可以接受的。

Unfortunately you can't get around it. 不幸的是你不能绕过它。

Even if your class definition looked like this: 即使您的类定义如下所示:

class A {
private:
     std::string* id;
public:
     void f();
};

then there's still not much you could do, as forward declaring std::basic_string<char, etc> is a pain in the ass. 那么你仍然没有多少能做,因为前方声明std::basic_string<char, etc>是一个痛苦的屁股。 I'm not even going to demonstrate. 我甚至不打算演示。

Fortunately, although using namespace std in headers is a definite no-no, you can usually get away with #include ing standard headers in your own headers, without worrying about it. 幸运的是,尽管在头文件中using namespace std是一个明确的using namespace std ,但你通常可以在自己的头文件中使用#include标准头文件,而不必担心它。

Possibly some crazy extern declaration would help, but that's not a way. 可能一些疯狂的extern声明会有所帮助,但这不是一种方式。 Why don't you want to include in header files? 为什么不想包含在头文件中?

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

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