Search code examples
c++listmultidimensional-arrayc++14

c++ 14 Categorized List of 3 Strings


My brain is fried this morning, and I am struggling to understand why I am getting a compiler error.

error: no matching constructor for initialization of 'list<data::tweak_cat>'

My code is this:

#include <list>
#include <string>

// fire up our std namespace
using namespace std;

namespace data {

    // create a structure to hold the tweak data
    struct the_tweak {
        string _name;
        string _value;
        string _type;
    };

    // create a structure to hold our category data
    struct tweak_cat {
        string _category;
        list< the_tweak > data;
    };

    class properties {
        public:
            // return our lists of strings for the tweak properties
            static list< tweak_cat > tweak_properties( ) {
                // hold the return
                list< tweak_cat > _ret = { 
                    { "debugging", 
                        { "testing", "0", "" }, 
                    },
                    { "dozing", 
                        { "testing2", "this", "" }, 
                        { "testing3", "that", "" }, 
                        { "testing4", "0", "theother" }, 
                    },
                };
                
                // return it
                return _ret;
            }
    };
}

I am building a cli based set of tweaks for my phone, in the process attempting to teach myself some c++ and compiling for it. Where am I going wrong, and how can I correct it?

It is my understanding (from reading) that lists are the more efficient method to "store" this type of data, over vectors and arrays... I initially had these as vector< vector< string > > without the category parent, and it worked ok...


Solution

  • You need to add more braces because the structure is:

    tweak_cat
    {
       string,
       list
       {
           the_tweak { name, value, type },
           the_tweak { name, value, type },
           the_tweak { name, value, type },
       }
    }
    

    Code:

    list<tweak_cat> _ret = {
        {
            "debugging",
            {
                {"testing", "0", ""}
            }
        },
        {
            "dozing",
            {
                {"testing2", "this", ""},
                {"testing3", "that", ""},
                {"testing4", "0", "theother"}
            }
        },
    };
    

    The above should compile.