Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

g++

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.

like image 769
BrainRenticus Avatar asked Apr 13 '26 15:04

BrainRenticus


1 Answers

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

like image 74
Amadeus Avatar answered Apr 19 '26 10:04

Amadeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!