如何解决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
中对此有更多了解感谢所有尝试提供帮助的人,感谢您的时间和关注。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。