Skip to content
Advertisement

Program can compile on Mac, but not Linux. Gets error: converting to from initializer list

I have a program which will successfully compile on my mac but when I try on my linux desktop it cannot. Instead, I get the following error:

plot.cpp:12:63: error: converting to ‘std::tuple<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >’ 
from initializer list would use explicit constructor 
‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& ...) 
[with _UElements = {const char (&)[4], const char (&)[6], const char (&)[6]}; 
<template-parameter-2-2> = void; _Elements = {std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >, std::basic_string<char,
 std::char_traits<char>, std::allocator<char> >}]’

like 12 of plot.cpp looks like this:

vector<tuple <string, string, string> >{
    {"yes", "Agree", "Empty"}, {"no", "Disagree", "Empty"}},

This vector is initialised in header.h, here:

class Menu
{
    std::string _name, _situation, _excuse;
    std::vector<std::tuple <std::string, std::string, std::string>> _choices;
    std::vector<std::string> _items;

    public:
        Menu(const std::string &name,
             const std::string &situation,
             const std::string &excuse,
             const std::vector<std::tuple <std::string, std::string, std::string>> &choices = std::vector<std::tuple <std::string,std::string, std::string> >{},
             const std::vector<std::string> &items = std::vector<std::string>{});
        virtual ~Menu ();
        const bool operator==(const std::string &name);
        const std::string& Explain_Choice();
        const void Enter_String();
        std::string choice;
        std::string ItemChoice;
        const void Present_Choice() ;
        const void No_Choice();
};

I have looked around a bit and I suspect the error is coming from the line in header.h where I say &choices =. However, my attempts to initialise this differently have not worked.

I have 2 questions. Firstly, why can this code compile on my mac but not on linux, is one of the compilers somehow ”wrong”? And then secondly, is there any suggestions on how I can change my code to allow it to compile on the Linux machine.

Advertisement

Answer

Something looks fishy here:

         ...
         const std::string &excuse,
         const std::vector<std::tuple <std::string, std::string, std::string>> &choices = std::vector<std::tuple <std::string,std::string, std::string> >{},
         const std::vector<std::string> &items = std::vector<std::string>{});
         ...

I know you can pass by reference with a default const parameter, but this definitely looks like code smell.

Based on the error message, it looks like this second line is where your error is coming from. It may be better in this case to overload the constructor instead of having these specific default arguments.

This answer has more information about this specific error (unordered_map, but the rational and explanation is the same).

In summary, this could be be a difference between default C++ standards the compilers are using. g++ uses std=c++11 by default, not sure what the OSX compiler uses. You may need to pass in std=c++14 to g++ to get the smarter default constructors for the std::vector<std::tuple<...> >.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement