为什么C ++ Windows Store应用程序头文件包含在cpp文件中?

时间:2015-02-04 13:22:14

标签: c++ windows-store-apps

编写最近的Windows应用商店应用程序,我很惊讶我可以使用std :: string类型而不必包含其标头。更具体地说,以下代码编译时没有任何错误:

StringExample.h:

#pragma once

class StringExample
{
public:
    std::string str;
};

StringExample.cpp:

#include "pch.h"

#include "StringExample.h"

#include <string>

从.cpp文件中删除字符串include会导致编译器失败。不应该在头文件中需要字符串头吗?

1 个答案:

答案 0 :(得分:0)

我的坏。我仔细检查了一个空项目中包含的顺序,并且.cpp文件确实需要看起来像这样:

#include "pch.h"

#include <string>

#include "StringExample.h"

因此,包括<string>允许StringExample.h进行编译,而删除include使其失败。

相关问题