gpt4 book ai didi

c++ - Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:12 24 4
gpt4 key购买 nike

我正在学习 Bjarne Stroustrup 的书“Programming - Principles and Practice using C++”。在他的书中,他要求包括“std_lib_facilities.h”。所以我得到了这样的部分代码

#ifndef H112
#define H112 020215L


#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>

//------------------------------------------------------------------------------
#if __GNUC__ && __GNUC__ < 5
inline ios_base & defaultfloat(ios_base& b) // to augment fixed and scientific as in C++11
{
b.setf(ios_base::fmtflags(0), ios_base::floatfield);
return b;
}
#endif

Xcode 不会编译我的项目并显示错误

  • unknown type name in this part

  • Unknown type name 'ios_base'; did you mean 'std::ios_base'? Replace 'ios_base' with 'std::ios_base'

inline ios_base & defaultfloat(ios_base& b)    

并显示错误

Use of undeclared identifier 'ios_base'; did you mean 'std::ios_base'?

b.setf(ios_base::fmtflags(0), ios_base::floatfield);

所以我将所有 ios_base 更改为 std::ios_base但它仍然不会编译我的项目....

最佳答案

这似乎是文件中的错误。该部分是“最近”添加的,应该只在版本 5 之前的 GCC 版本下运行,但是 ios_base 在 using 指令 ​​ 之前被不合格地使用。

“正确”的修复是用 std:: 限定所有 ios_base 的使用:

inline std::ios_base & defaultfloat(std::ios_base& b)
{
b.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
return b;
}

或者,您可以将 using namespace std; 语句移到文件中的代码片段之前。这不是一个很好的解决方案,但 Bjarne 关于此 header 的观点并不是为了展示良好的编码实践,而是为了向初学者隐藏一些复杂性。

无论如何,只要您已经取得足够的进展,就应该完全停止使用 header 并正确地做事。这本书告诉你什么时候。

关于c++ - Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53060662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com