c++ - 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
谢谢你。
解决方案
我以错误的顺序包含标题。在 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"
关于这个问题的更多信息
感谢所有试图提供帮助的人,感谢您的时间和关注。
推荐阅读
- ruby-on-rails - 通过关联表对索引进行排序?
- ios - 在 XCode 中使用类似 AWK 的处理
- haskell - 如何为 kind (* -> *) -> * 类型编写 aeson ToJSON 实例
- python - Twisted Command 构建失败的轮子
- windows - 从 Windows 为 Raspberry Pi 3 B 交叉编译裸机 Rust
- python - Keras:完成训练过程后释放内存
- pip - conda 相当于 pip install --user
- java - JanusGraph 添加边缘似乎不起作用
- typescript - 在 VSCode 中移动 Typescript 文件时自动导入重构?
- python - IPython 控制台可以在 Eclipse/Pydev 中进行交互吗?