c++ - 错误 : 'std::string_view' has not been declared

标签 c++ g++

我正在尝试从一本书中编译一个示例问题。

当我在我的系统上编译时出现错误,所以我尝试了一个在线编译器并且它工作了。我将 g++ 更新到版本 9 并再次尝试,但它仍然无法编译。 我收到错误消息 'std::string_view' has not been declared

// Sorting words recursively
#include <iostream>
#include <iomanip>
#include <memory>
#include <string>
#include <string_view>
#include <vector>

using Words = std::vector<std::shared_ptr<std::string>>;

// Function prototypes
void swap(Words& words, size_t first, size_t second);
void sort(Words& words);
void sort(Words& words, size_t start, size_t end);
void extract_words(Words& words, std::string_view text, std::string_view separators);
void show_words(const Words& words);
size_t max_word_length(const Words& words);

第一个错误发生在 extract_words 原型(prototype)中,之后所有尝试使用 text 参数,或任何使用 std::string_view,导致错误。

最佳答案

cppreference.com 中所述, std::string_view 仅适用于 c++17 或更新版本。

要使用它,请在您的编译器中启用它。对于 g++ 或 clang++,使用开关 -std=c++17

关于c++ - 错误 : 'std::string_view' has not been declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295334/

相关文章:

c++如何从派生类调用派生类中重载的模板类函数?

c++ - 为什么 assert 定义为 (void)0?

c++ - if 语句有问题。 (新程序员。)

c++ - RVO/NRVO 和公共(public)未定义复制构造函数

c++ - g++和gcc有什么区别?

c++ - C 字符串堆栈分配

c++ - 如何将行号添加到 QTreeView?

c++ - 哪些是最常用的 gcc/g++ 信息标志(不仅仅是优化)

c++ - g++ 没有生成文件

python - Cython 的 C++ API 声明失败