Visual Studio无法识别标准库

问题描述

我正在用C ++在Visual Studio 2015上编写MFC应用程序。我添加了一些代码,这些代码使用std库的成员并假定为一个int并从中创建带有前缀“ 0x”的十六进制char *。我试图从两台不同的计算机上在VS 2015和VS 2017上构建项目,但遇到了相同的错误-VS无法识别std库。我已经在其他程序(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

谢谢。

解决方法

我以错误的顺序包含了标题。在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"

在此question

中对此有更多了解

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...