我正在尝试从一本书中编译一个示例问题。
当我在我的系统上编译时出现错误,所以我尝试了一个在线编译器并且它工作了。我将 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/