BrainRenticus
BrainRenticus

Reputation: 225

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

I am trying to compile an example problem from a book.

I got errors when I compiled on my system, so I tried an online compiler and it worked. I updated g++ to version 9 and tried again, but it still won't compile. I get the error '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);

First error occurs in the extract_words prototype, and all attempts after that to use the text parameter, or any use of std::string_view, causes an error.

Upvotes: 2

Views: 8387

Answers (2)

GZs
GZs

Reputation: 21

After a day and a half of compiler upgrades, downgrades, frantically messing with parameter settings, including, excluding files and directories, reading dozens of blogs and following scores of guidance this was the solution: use the switch -std=c++17 Thanks to Amadeus!

Upvotes: 0

Amadeus
Amadeus

Reputation: 10665

As noted in cppreference.com, std::string_view is only available in c++17 or newer.

In order to use it, enable it in your compiler. For g++ or clang++, use the switch -std=c++17

Upvotes: 14

Related Questions