Share via

Linker error when trying to use std::bitset

Osman Zakir 121 Reputation points
2021-11-10T20:45:38.487+00:00

Hi.

I have this code:

// Osman Zakir  
// 11 / 10 / 2021  
// Beginning C++20: From Novice to Professional by Ivor Horton and Peter Van Weert  
// Chapter 4 Exercise 7  
// Exercise Specs:  
/**  
 * Implement a program that prompts for the input of a letter. Use a library function  
 * to determine whether the letter is a vowel and whether it is lowercase or not, and output the  
 * result. Finally, output the lowercase letter together with its character code as a binary value. As  
 * a bonus exercise, you could try to do the latter without the use of std::format()?  
 */  
  
import <iostream>;  
import <bitset>;  
#include <cctype>  
  
int main()  
{  
    std::cout << "Enter a letter: ";  
    char letter{};  
    std::cin >> letter;  
    std::cin.ignore(32767, '\n');  
  
    const std::bitset<8> binary_letter{ static_cast<std::uint16_t>(std::tolower(letter)) };  
    if (std::isalpha(letter))  
    {  
        if (std::tolower(letter) == 'a' || std::tolower(letter) == 'e' ||  
            std::tolower(letter) == 'i' || std::tolower(letter) == 'o' ||  
            std::tolower(letter) == 'u')  
        {  
            std::cout << "Your letter is a vowel." << std::endl;  
            if (std::islower(letter))  
            {  
                std::cout << "It's lowercase, and in binary, it's " << binary_letter << std::endl;  
            }  
        }  
        else  
        {  
            std::cout << "Your letter is a consonant." << std::endl;  
            if (std::islower(letter))  
            {  
                std::cout << "It's lowercase, and in binary, it's " << binary_letter << std::endl;  
            }  
        }  
    }  
}  

I'm getting this linker error from it:

error LNK2019: unresolved external symbol "class std::ctype<char> const & __cdecl std::use_facet<class std::ctype<char> >(class std::locale const &)" (??$use_facet@V?$ctype@D@std@@@std@@YAABV?$ctype@D@0@ABVlocale@0@@Z) referenced >in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<char,struct >std::char_traits<char>,8>(class std::basic_ostream<char,struct std::char_traits<char> > &,class std::bitset<8> const &)" (??$?>6DU?$char_traits@D@std@@$07@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@Abv ?>$bitset@$07@0@@Z)
1>E:\programming\visual_studio_2019\Projects\beginningcpp20\chapter4ex7\Debug\chapter4ex7.exe : fatal error LNK1120: 1 >unresolved externals
1>Done building project "chapter4ex7.vcxproj" -- FAILED.

Trying to build it on godbolt shows that it should build, but I've already tried restarting VS2019 and cleaning and rebuilding it with no luck.

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.


Answer accepted by question author

RLWA32 52,571 Reputation points
2021-11-11T01:10:22.853+00:00

Setting "Translate Includes to Imports" to Yes also allowed the posted code to build successfully.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. RLWA32 52,571 Reputation points
    2021-11-11T09:26:40.05+00:00

    In addition to my previous suggestions, adding #include for locale header will also enable successful build -

    import <bitset>;
    import <iostream>;
    #include <locale>
    #include <cctype>
    

    Was this answer helpful?

    0 comments No comments

  2. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2021-11-11T05:34:25.27+00:00

    Hi @Osman Zakir ,

    The program can run when I replace import <iostream>;import <bitset>; with #include<iostream> #include<bitset>, I suggest you try this method.

    148463-re.png

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.