初始化C ++结构的正确方法是什么?

时间:2020-07-04 06:56:18

标签: variables initialization member

我是编程新手。我收到警告了

C26495变量'Person :: num'未初始化。总是初始化一个 成员变量(类型6)。

下面是我的代码:

#include <iostream>

struct Person {
    int num;
    std::string color;
    std::string name;
};

int main() {        
    Person p1 = { 7,"Gray","Ben" };

    std::cout << p1.color << std::endl;

    Person p2;
    p2.num = 42;
    p2.color = "Blue";
    p2.name = "Connor";

    std::cout << p2.name << std::endl;

    Person p3 = { 3,"Magenta","Lucy" };

    Person* p3ptr = &p3;

    std::cout << p3ptr->num << p3ptr->color << p3ptr->name << std::endl;

    return 0;
}

还是不知道为什么我得到这个警告。代码运行正常,但是谁喜欢警告?

0 个答案:

没有答案