c++ 14 3 个字符串的分类列表

c++ 14 Categorized List of 3 Strings

提问人:Kevin 提问时间:10/5/2023 最后编辑:mchKevin 更新时间:10/5/2023 访问量:57

问:

今天早上我的大脑被炸了,我正在努力理解为什么我会遇到编译器错误。

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

我的代码是这样的:

#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;
            }
    };
}

我正在为我的手机构建一组基于cli的调整,在这个过程中,我试图自学一些c++并对其进行编译。我哪里出了问题,我该如何纠正?

我的理解(来自阅读)列表是在向量和数组上“存储”此类数据的更有效方法......我最初没有类别父级,它工作正常......vector< vector< string > >

C++ 列表 多维数组 C++14

评论

1赞 kiner_shah 10/5/2023
对于“打瞌睡”,您提供了 3 个列表,而预期为 1 个。你想在那里做嵌套列表。实际上,这同样适用于“调试”。
3赞 wohlstad 10/5/2023
旁注:为什么“使用命名空间 std;”被认为是不好的做法?
5赞 wohlstad 10/5/2023
列表是”存储“此类数据的更有效方法,而不是向量和数组” - 你为什么这么认为?
1赞 Andrej Podzimek 10/5/2023
(1) 您缺少列表初始值设定项的级别。这是最明显的错误。(2)使用C++20和指定的初始化器;它将使错误明显,初始化不易出错等。 (3)请删除;这是一种反模式。(除非您拥有(整个)命名空间,否则切勿使用它们。{}tweak_cat::datausing namespace std;
0赞 Kevin 10/5/2023
@AndrejPodzimek 不能使用 c++20。为 Android 9+ wohlstad 编译它 是的,我把它放在里面是为了简单回答这个问题。

答:

4赞 kiner_shah 10/5/2023 #1

您需要添加更多大括号,因为结构为:

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

法典:

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

以上应该编译。

评论

1赞 Kevin 10/5/2023
谢谢你提供额外的脑细胞。这确实做到了。:)