Home > Net >  Class std::locale has no member "imbue"
Class std::locale has no member "imbue"

Time:12-10

I tried to implement functionality so "-" character to be treated as whitespace when inputting with >> operator in c .

A tutorial I read said that I could use the "imbue()" method to do that, but in Visual Studio I'm getting the error that std::locale doesn't have the member imbue.

#include <iostream>
#include <string>
#include <locale>

int main()
{
   // Create a custom locale that treats the "-" character as whitespace
   std::locale custom_locale;
   custom_locale.imbue(locale(), new ctype<char>{ 0, 0, 0, 0, " -" });

   // Read a string from the user, using the custom locale
   std::string input;
   std::cin.imbue(custom_locale);
   std::cin >> input;

   // Print the input string
   std::cout << input << std::endl;
}

CodePudding user response:

imbue() was removed from std::locale in C 11. You can create a default global local, then use cin.imbue() and cout.imbue().

#include <iostream>
#include <string>
#include <locale>

int main()
{
  // Create a default global locale
  std::locale global_locale(std::locale::global(std::locale("")));

  // Create a new locale based on the default global locale
  std::locale custom_locale(global_locale);

  // Read a string from the user, using the custom locale
  std::string input;
  std::cin.imbue(custom_locale);
  std::cin >> input;

  // Print the input string
  std::cout.imbue(custom_locale);
  std::cout << input << std::endl;

  return 0;
}

CodePudding user response:

As mentioned by @Kyle Swecker in his answer, imbue() was removed from std::locale in C 11. Hence, you need to use std::locale constructure.

https://en.cppreference.com/w/cpp/locale/locale/locale

Also, the std::ctype < char > object is created incorrectly. Please refer below links and examples within them.

https://en.cppreference.com/w/cpp/locale/ctype_char/ctype https://en.cppreference.com/w/cpp/locale/ctype_char

Here is my solution. Below code treats '-' as whitespace.

#include <iostream>
#include <string>
#include <locale>
#include <vector>
// This ctype facet classifies - as whitespace
struct custom_whitespace : std::ctype<char> {
static const mask* make_table()
{
    // make a copy of the "C" locale table
    static std::vector<mask> v(classic_table(), classic_table()   table_size);
        v['-'] |= space;  // '-' will be classified as whitespace
        v[' '] &= ~space;      // space will not be classified as whitespace
        return &v[0];
    }
    custom_whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) {}
};

int main()
{
    // Create a custom locale that treats the "-" character as whitespace
    // Create a custom locale that treats the "-" character as whitespace
    std::locale custom_locale(std::locale(), new custom_whitespace);

    // Read a string from the user, using the custom locale
    std::string input;
    std::cin.imbue(custom_locale);
    std::cin >> input;

    // Print the input string
    std::cout << input << std::endl;
}
  • Related