如何在 C++ 中支撑自定义类的初始化向量?

发布于 2025-01-20 02:56:24 字数 710 浏览 3 评论 0原文

拥有此简单的代码:

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

class Person{
public:
    Person(std::string const& name) : name(name) {}
    std::string const& getName() const {
        return name;
    }
private:
    std::string name;
};

int main(){
//    std::vector<int> ar = {1,2,3};
    std::vector<Person> persons = {"John", "David", "Peter"};
}

我会遇到错误:

could not convert ‘{"John", "David", "Peter"}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<Person>’

那么为什么启用int s的向量来支撑initialize,但是使用隐式构造函数自定义类(带有std :: String :: String)不能?以及如何启用它?

Having this simple code:

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

class Person{
public:
    Person(std::string const& name) : name(name) {}
    std::string const& getName() const {
        return name;
    }
private:
    std::string name;
};

int main(){
//    std::vector<int> ar = {1,2,3};
    std::vector<Person> persons = {"John", "David", "Peter"};
}

I am getting error:

could not convert ‘{"John", "David", "Peter"}’ from ‘<brace-enclosed initializer list>’ to ‘std::vector<Person>’

So why vector of ints is enabled to brace-initialize, but custom class with implicit constructor (with std::string) cannot? And how to enable it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韵柒 2025-01-27 02:56:24

您只需要更多的牙套:

std::vector<Person> persons = {
    {"John"}, {"David"}, {"Peter"}
};

You just need more braces:

std::vector<Person> persons = {
    {"John"}, {"David"}, {"Peter"}
};
平定天下 2025-01-27 02:56:24

问题是您在初始化列表中使用(C 风格)字符串文字,但向量包含 std::string。解决此问题的一种方法是传递 std::string 对象。这是一种方法:

int main(){
    using namespace std::string_literals;
    std::vector<Person> persons = {"John"s, "David"s, "Peter"s};
}

或者,您可以为每个对象使用大括号初始值设定项,如另一个答案中所建议的:

int main(){
    std::vector<Person> persons = {{"John"}, {"David"}, {"Peter"}};
}

The issue is that you are using (C-style) string literals in the initialization list, but the vector holds std::string. One way to work around this is to pass std::string objects. Here is one way to do it:

int main(){
    using namespace std::string_literals;
    std::vector<Person> persons = {"John"s, "David"s, "Peter"s};
}

Alternatively, you can use brace initializers for each object, as suggested in another answer:

int main(){
    std::vector<Person> persons = {{"John"}, {"David"}, {"Peter"}};
}
风向决定发型 2025-01-27 02:56:24

您可以向您的类添加另一个接受 const char* 的构造函数,它应该可以工作

class Person{
public:
    Person(const char* name): Person(std::string(name)) {}
    Person(std::string const& name) : name(name) {}
    std::string const& getName() const {
        return name;
    }
private:
    std::string name;
};

或附加大括号,就像另一个答案中建议的那样。

You can add another constructor accepting const char* to your class and it should work

class Person{
public:
    Person(const char* name): Person(std::string(name)) {}
    Person(std::string const& name) : name(name) {}
    std::string const& getName() const {
        return name;
    }
private:
    std::string name;
};

Or additional braces, like suggested in another answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文