Thread: Variant to replace a union but is “undeclared identifier”

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    32

    Variant to replace a union but is “undeclared identifier”

    Greetings from sunny London. I know some C, have written in Lazarus Pascal, enjoy coding for pleasure, and decided to dabble in C++. Lots of terminology still escapes me.

    I was experimenting with structures and unions and quickly realised that unions do not handle strings well and according to web searches, I should consider using “#include <variant>” and “std::variant<int, std::string> var; var = "hello";”
    But the latter line produces a series of errors which seem to cluster around “C2039 ‘variant’: is not a member of ‘std’”
    and
    “C2065 ‘variant’: undeclared identifier”

    un-commenting using namespace std; does not improve the situation.

    Any help appreciated

    Stephanos



    Code:
    // StructuresV2-2.cpp
    // OS - Windows 10 Enterprise
    // Compiler - C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x64>cl.exe
    // Microsoft(R) C / C++ Optimizing Compiler Version 19.26.28806 for x64
    // Copyright(C) Microsoft Corporation.All rights reserved.
    // usage: cl[option...] filename...[/ link linkoption...]
    // C : \Program Files(x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\Hostx64\x64 >
    // Application - MS Visual Studio Community 201916.6.3
    // Experiement to see if a union can be a field in a structure
    // It can.  But unions limited ability to store/use string.
    // Use: 
    //   #include <variant>
    //   std::variant 
    // to replace union
    using namespace std;
    #include <iostream>
    #include <variant>
    #include <string>  // needed to use std::to_string()
    struct coffeeBean
    {
        std::string name; std::string country; int strength; union internationalgrade;
    } newBean; //
    int main()
    {
        newBean.name = "Flora";
        newBean.country = "Mexico";
        newBean.strength = 9;
        std::cout << "Bean: " + newBean.name + " From: " + newBean.country;
        std::cout << " Strength: " + std::to_string(newBean.strength) << std::endl;
        std::variant<int, std::string> var; var = "hello";
    
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    std::variant - cppreference.com
    You need to tell your compiler to compile in C++17 mode.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Dear Salem

    Thank you

    But how? This application, MS Visual Studio Community 201916.6.3 is harder to use than Code::Blocks. I have looked around and cannot see how to do that.

    In Code::Block the compiler is GNU GCC Compiler. There are 3 that start Microsoft Visual C++, 1) Toolkit 2003, 2) 2005-2008, 3) 2010. If I chose either of these I will not know how to change the Toolchain Executable tab.

    Thanks

    Stephanos

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Dear Salem

    Thanks, I made the change and it builds. Next I added a cout:
    Code:
    std::cout << "Content of var is: " + std::to_string(var);
    but this led to an error:
    “C2665 'std::to_string': none of the 9 overloads could convert all the argument types”
    So I decided to see if an integer would print out. I changed the value of ‘var’
    Code:
    std::variant<int, std::string> var; var = 99;
    which did build, and added a cout
    Code:
    std::cout << "Content of var is: " + std::to_string(var);
    I got the same error message:
    “C2665 'std::to_string': none of the 9 overloads could convert all the argument types”

    Am I wasting my time? Eventually I want to use a variant inside a structure. If this is silly because I do not know something I am happy to abandon the plan.

    Any further help appreciated

    Stephanos

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe say which variant you mean.
    Code:
    std::cout << "Content of var is: " + std::to_string(std::get<int>(var));
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    32
    Dear Salem

    Thanks, this is now solved thanks to your last suggestion.
    Here is the code to declare a variant, assign integer data to it and then print out
    Code:
    std::variant<int, std::string> var; var = 99; 
    std::cout << "Content of var is: " + std::to_string(std::get<int>(var));
    Here is the code to declare a variant, assign a string data to it and then print out
    Code:
    std::variant<int, std::string> var; var = "Hello";
    std::cout << "Content of var is: " + std::get<std::string>(var);
    I have learned much, especially about ‘get’ and putting things in <>.

    May all your code run smoothly

    Stephanos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. InetNtop undeclared identifier
    By Vana Papi in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2012, 03:05 PM
  2. Undeclared Identifier
    By Kayoss in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 10:48 AM
  3. RegGetValue' : undeclared identifier?
    By Sephiroth222 in forum C++ Programming
    Replies: 9
    Last Post: 09-25-2005, 05:05 AM
  4. SHGetFolderPath undeclared identifier
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2003, 07:54 PM
  5. Undeclared Identifier
    By Witch_King in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2001, 12:58 PM

Tags for this Thread