首页 > 解决方案 > Visual Studio 无法识别标准库

问题描述

我正在用 C++ 在 Visual Studio 2015 上编写 MFC 应用程序。我添加了一些使用 std 库成员的代码,并假设采用一个 int 并从中创建一个带有前缀“0x”的十六进制 char*。我尝试从两台不同的计算机上在 VS 2015 和 VS 2017 上构建项目,但我得到了相同的错误 - VS 无法识别标准库。我已经在其他程序(Clion)上运行代码,它运行良好。

当我包含时,#include <stdlib>我收到以下错误: cannot open source file "stdlib"

我已经尝试重新安装 VS,并检查了我是否拥有支持 C++ 的所有必要扩展,但我想仍然缺少一些东西。我该如何解决?

编码:

std::ostringstream ss;
int i = 7;

ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();

std::cout << output << std::endl;

并包括以下标题:

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>

我收到以下错误:

'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier

谢谢你。

标签: c++visual-studiostd

解决方案


我以错误的顺序包含标题。在 Visual Studio 中的每个 C++ 项目中,它都会"stdafx.h"自动包含库。该库包含许多常用的库,例如<string>等。解决方案是按以下方式编写包含:

#include "stdafx.h"
// other headers of the form "header.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>

代替:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>

#include "stdafx.h"
// other headers of the form "header.h"

关于这个问题的更多信息

感谢所有试图提供帮助的人,感谢您的时间和关注。


推荐阅读