error: 'std::string_view' has not been declared

error: 'std::string_view' has not been declared

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

我在我的系统上编译时遇到错误,所以我尝试了一个在线编译器,它成功了。我将 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 原型中,此后所有尝试使用 text 参数或使用 std::string_view 都会导致错误。

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

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

经过一天半的编译升级,降级,疯狂的乱搞参数设置,包括,排除文件和目录,看了几十篇博文,跟着几十篇攻略使用开关-std=c++17 感谢 Amadeus!