首页 > 解决方案 > 为什么编译器找不到类 std::ifstream?

问题描述

我有一个 VS2013 VC++ 项目,我想创建一个 std::ifstream 对象,如下所示:

#include "stdafx.h"
#include <fstream>

int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream f;
    return 0;
}

它按预期工作,但如果我更改包含的标头顺序,编译器将发出错误:

#include <fstream>
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    std::ifstream f;
    return 0;
}
error C2065: 'ifstream' : undeclared identifier

stdafx.h 文件中的内容非常简单:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

我的问题是为什么包含的标头顺序会影响编译器查找 std::ifstream?stdafx.h 包含的其他头文件中是否定义了任何宏会影响结果?但是当我在 ifstream 类上按 F12 并跳转到定​​义时,该类没有变灰,这意味着它们已启用并且编译器可以找到它们对吗?我有一段时间谷歌,但仍然无法弄清楚为什么它编译失败。

标签: c++visual-c++

解决方案


推荐阅读