添加字符串向量和不同数据类型向量的映射 [英] Adding a map of string vector and different datatype vector

查看:34
本文介绍了添加字符串向量和不同数据类型向量的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://stackoverflow.com/a/44884240/3782963 中所建议.我创建了一个返回类型 std::map 的方法;get_data1().我创建了一个名为 datastruct:

As suggested in https://stackoverflow.com/a/44884240/3782963. I created a method of return type std::map<std::string, Spikes::data> get_data1(). I create a struct called data:

struct data {

            std::vector<Eigen::MatrixXd> samples;
            std::vector<std::string> sample_names;
            int index = -1;
        };

并将它放在我的命名空间FileReader类之外.

and placed it in my namespace and out of the class 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 中,我将此方法称为:

And in my main.cpp I am calling this method as:

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

我不知道发生了什么.

我什至尝试过:

content["samples"] = Spikes::data{samples,0};
content["sample_names"] = Spikes::data{sample_names,1};

我仍然遇到同样的错误.任何帮助将不胜感激.

I still get the same error. Any help would be highly appreciated.

推荐答案

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

As suggested in https://stackoverflow.com/a/44884240/3782963. I created a method of return type std::map get_data1().

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

The suggested answer clearly states that you need to implement constructors for data:

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

    std::vector<Eigen::MatrixXd> samples;
    std::vector<std::string> names;
    int index = -1;
};

@SamVarshavchik 很好地描述了原因.

@SamVarshavchik described the reason well.

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

I assume your intention would be having constructors as follows:

// 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}));

这篇关于添加字符串向量和不同数据类型向量的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆