2

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.

JaMiT
  • 14,422
  • 4
  • 15
  • 31
BrainRenticus
  • 215
  • 3
  • 6
  • Possible duplicate of [error: 'string\_view' is not a member of 'std'](https://stackoverflow.com/questions/58229919/error-string-view-is-not-a-member-of-std) – JaMiT Oct 09 '19 at 00:58

2 Answers2

13

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

Amadeus
  • 10,199
  • 3
  • 25
  • 31
0

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!

GZs
  • 21
  • 1