【问题标题】:Conversion Constructor using map argument giving Errors使用映射参数的转换构造函数给出错误
【发布时间】:2021-09-30 08:13:48
【问题描述】:

我制作了一个小型控制台程序,它有一个名为 Class_Room 的类。我可以用它制作学校教室的对象,并在其中存储学生姓名、卷号和年龄。学生数据存储在地图中。我想做这样的教室对象:

Class_Room class8C = {
       {"student x", 2},  //2 for rollnumber
       {"student y", 3}
};

所以我做了一个转换构造函数:

Class_Room(std::map<int, std::string> map) {
        for (std::pair<int, std::string> value : map) {
            student_data[value.first] = { value.second, "NIL" };
        }
    }

但我得到错误: 没有构造函数 Class_Room::Class_Room 的实例与参数列表匹配

“正在初始化”:无法将“初始化列表”转换为 Class_Room

这是我的全部代码:

#include <iostream>
#include <vector>
#include <string>
#include <map>

class Class_Room {
public:

    Class_Room(std::map<int, std::string> map) {
        for (std::pair<int, std::string> value : map) {
            student_data[value.first] = { value.second, "NIL" };
        }
    }

    void new_student(int roll, std::string name, int age = 0)
    {
        data.clear();
        data.push_back(name);
        if (age != 0) {
            data.push_back(std::to_string(age));
        }
        else {
            data.push_back("NIL");
        }
        student_data[roll] = data;
    }

    std::map<int, std::vector<std::string>> get_student_data_map()
    {
        return student_data;
    }


private:
    std::map<int, std::vector<std::string>> student_data;
    std::vector<std::string> data;
};

std::ostream& operator<< (std::ostream& output, Class_Room classroom)
{
    std::map<int, std::vector<std::string>> student_data = classroom.get_student_data_map();

    for (std::pair<int, std::vector<std::string>> value : student_data) {
        output << value.first << " => " << value.second[0] << ", " << value.second[1] << "\n";
        
    }
    return output;
}


int main() {
    Class_Room class8C = {{"Ihsan", 2}};
    std::cout << class8C;
}

我是 C++ 的初学者,不知道出了什么问题,任何帮助将不胜感激。

【问题讨论】:

  • 您有错字和缺少外括号:Class_Room class8C = { {{2, std::string("Ihsan")}}};

标签: c++


【解决方案1】:

这个答案还有更多内容,但我会选择可以解决您问题的内容,然后会尝试解释一些更有趣的部分,以便您从中学到一些东西。

GIST:你的地图正好相反

// your map has keys of type int, values of type string
Class_Room(std::map<int, std::string> map) { // pairs of <int, string>
        for (std::pair<int, std::string> value : map) {
            student_data[value.first] = { value.second, "NIL" };
        }
}

// and then you want to insert pairs of <string, int>
// keys of type string, values of type int
// like: {"Ihsan", 2}

颠倒这些应该会让你走上正轨。

// Class_Room class8C = {{2, "Ihsan"}}; // but this is not enough!
// you actually need something along these lines:
    Class_Room class8C{
        std::map<int, std::string>{{2, "Ihsan"}}};

// or

    Class_Room class8C{ 
        { // creates the map
            { // creates the pair to insert
                2, std::string("Ihsan") 
            }
        }
    };

你得到的错误是因为编译器试图找到正确的构造函数并且它失败了。

首先,您需要一个映射来传递给您的构造函数。编译器必须想办法在调用你的 ctor 之前创建该映射(如果该映射尚不存在)。

如果您查看 std::map 的构造函数,您会注意到第 5 个采用初始化列表:https://en.cppreference.com/w/cpp/container/map/map

该初始化列表的 value_type 模板也是:,与映射本身相同。所以顺序很重要。

您也可以事先创建地图,但我想您想要简洁的代码。

【讨论】:

  • 感谢您的帮助,这就是问题所在,我把 int 和 string 颠倒过来了,还忘了多放一组花括号
  • 小建议,如果你对它感兴趣,你似乎是按值传递地图,这意味着你正在完全复制地图。您可以使用从临时地图中移动到move construct 的元素steal。或者只是在转换 ctor 中使用 const&amp;
  • 另外,您可以修改您正在使用的迭代代码以避免一些副本,请参阅其他答案:stackoverflow.com/questions/26991393/…
猜你喜欢
  • 2011-12-29
  • 1970-01-01
  • 2021-10-24
  • 2011-06-19
  • 2016-10-26
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多