添加字符串向量和不同数据类型向量的映射

时间:2017-07-04 02:40:37

标签: c++ vector

正如https://stackoverflow.com/a/44884240/3782963中所述。我创建了一个返回类型为std::map<std::string, Spikes::data> get_data1()的方法。我创建了一个名为struct的{​​{1}}:

data

并将其放在我的struct data { std::vector<Eigen::MatrixXd> samples; std::vector<std::string> sample_names; int index = -1; }; 和课堂namespace之外。

我的方法如下:

FileReader

在我的std::map<std::string, Spikes::data> FileReader::get_data1() { std::map<std::string, Spikes::data> content; std::vector<Eigen::MatrixXd> samples; std::vector<std::string> sample_names; for (auto i : list_dir()) { if (contains_number(i)) { samples.push_back(load_csv(location + "/" + i)); sample_names.push_back(i); } } content.emplace(std::make_pair("samples", Spikes::data{samples,0})); content.emplace(std::make_pair("sample_names", Spikes::data{sample_names,1})); return content; 中,我将此方法称为:

main.cpp

但我收到以下错误:

for (auto i:a.get_data1()["samples"].samples){
        std::cout<<i;
    }

我不知道发生了什么。

我甚至尝试过:

/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.cpp: In member function 'std::map<std::basic_string<char>, Spikes::data> Spikes::FileReader::get_data1()':
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.cpp:154:69: error: no matching function for call to 'Spikes::data::data(<brace-enclosed initializer list>)'
     content.emplace(std::make_pair("samples", Spikes::data{samples,0}));
                                                                     ^
In file included from /cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.cpp:3:0:
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data()
     struct data {
            ^
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 0 arguments, 2 provided
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data(const Spikes::data&)
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 1 argument, 2 provided
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data(Spikes::data&&)
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 1 argument, 2 provided
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.cpp:155:79: error: no matching function for call to 'Spikes::data::data(<brace-enclosed initializer list>)'
     content.emplace(std::make_pair("sample_names", Spikes::data{sample_names,1}));
                                                                               ^
In file included from /cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.cpp:3:0:
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data()
     struct data {
            ^
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 0 arguments, 2 provided
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data(const Spikes::data&)
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 1 argument, 2 provided
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note: candidate: Spikes::data::data(Spikes::data&&)
/cygdrive/c/Users/aksha/Box Sync/MyDrive/Projects/Akshay-GitHub/libSpikes/libSpikes/src/Core/FileReader.h:13:12: note:   candidate expects 1 argument, 2 provided

我仍然得到同样的错误。任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:1)

对简单结构/聚合/等使用值初始化时...:

Spikes::data{samples,0}

您必须初始化此结构的每个成员。

您的data类包含三个成员,前两个是向量,第三个成员是int。显然,使用普通整数std::vector初始化std::string 0 s不会随处可见。此0将被解释为初始化struct的第二个成员的值,因为它是初始化列表中的第二个值。 struct的第二个成员是std::vector std::string,编译器基本上会尝试相应地初始化第二个成员,如下所示:

std::vector<std::string> sample_names{0};

这是你初始化的尝试归结为,这不会走得太远......

使用值初始化时,不能随意跳过类成员。您必须按顺序初始化每个类成员。在这种情况下,你可能的意图可能是这样的:

Spikes::data{samples,{},0}

用一个空的子初始化列表初始化第二个向量。同样,您第二次尝试初始化:

Spikes::data{sample_names,1}

它遇到了同样的问题。你可能的意图可能是:

Spikes::data{{}, sample_names,1}}

即。使用空的初始化列表初始化第一个std::vector成员,然后相应地初始化第二个std::vector

答案 1 :(得分:1)

  

正如https://stackoverflow.com/a/44884240/3782963中所述。我创建了一个返回类型为std :: map get_data1()的方法。

建议的答案明确指出您需要为data实施构造函数

struct data 
{
    // constructors... <- here

    std::vector<Eigen::MatrixXd> samples;
    std::vector<std::string> names;
    int index = -1;
};
@SamVarshavchik很好地描述了这个原因。

我假设您的意图是如下构造函数:

// Constructors
data(std::vector<Eigen::MatrixXd> && rhs, int idx) 
{
    samples = std::move(rhs);
    index = idx;
}

data(std::vector<std::string> && rhs, int idx) 
{
    samples = std::move(rhs);
    index = idx;
}

content.emplace(std::make_pair("samples", Spikes::data{ std::move(samples), 0}));
content.emplace(std::make_pair("sample_names", Spikes::data{ std::move(sample_names), 1}));