Defining structure variable after structure type

后端 未结 3 670
眼角桃花
眼角桃花 2020-12-22 03:53
#include 

struct people 
{
    int id;
} person; // that part

int main()
{
    person = {3};   
    std::cout << person.id;

    return 0;
}
         


        
3条回答
  •  渐次进展
    2020-12-22 04:28

    You can't use the structure initialization syntax in a normal assignment, it has to be done when you declare your variable:

    struct people 
    {
        int id;
    } person = { 3 };
    

    If you have a C++11 compatible compiler, you can use the uniform initialization syntax to copy later though:

    person = people { 3 };
    

提交回复
热议问题