简体   繁体   English

在STL C ++中包含头文件

[英]Including Header files in c++ of STL

I have a class Like this 我有这样的课

/*class.h*/

class MClass
{
 public:
   MClass(std::vector<int> number);

}

And

/* class.cpp */
#include <vector>
#inclue "class.h"
MClass::MClass(std::vector<int> number)
{
  // Do Something
}

And it is not compiled if I do not add #include <vector> in the header file Is this behaviour normal or am I missing something? 如果不在头文件中添加#include <vector> ,则不会编译该行为是否正常?是否丢失了某些内容?

You should include <vector> in your header. 您应该在标题中包含<vector> And then you should include your class's header in your cpp file. 然后,您应该在cpp文件中包含类的标头。

You can't compile your cpp file without your class header. 没有类标题,就无法编译cpp文件。 So it's normal to include your class header - there's no way around it. 因此,包含您的类标头是正常的-无法解决。 Also, it's normal to include only what is needed in your header so it doesn't introduce unnecessary dependencies in code that use your header. 另外,通常只在标头中包含所需的内容,这样就不会在使用标头的代码中引入不必要的依赖关系。 In this case, given your member function signature, you are required to include <vector> in your header. 在这种情况下,给定成员函数签名,您需要在标头中包含<vector>

class.cpp should #include "class.h" class.cpp应该#include“ class.h”

class.h should #include <vector> class.h应该#include <vector>

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

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