编译器报错or告警---未初始化的变量

来源:互联网 发布:手游刷枪软件 编辑:程序博客网 时间:2024/06/05 01:59

现象:

vs开 SDL ,编译示例代码,按照逻辑 s_test应该是被分配空间了的,但是会报错。

观察实验:

这时候手动加个默认构造函数会过。成员变量声明方式改为  int c =1;也会过。

原理:

定义声明---大概是指这个过程,分配空间,赋初始值。有编译器赋初始值和码农赋初始值两种。有时候,为了保证码农知道自己做了什么,编译器会在使用未人工赋值的变量时,告警或者报错。

结论:需要告诉编译器我知道我干了什么。

示例代码:

#include <iostream>

using namespace std;
struct test
{
    int *a;
    int *b;
    int c;
};
int main()
{
    int ia = 1;
    int ib = 2;
    int ic = 3;
    test s_test;
    //std::is_trivial<test>::value;
    cout << std::is_trivial<test>::value<<endl;
    s_test.a = &ia;
    s_test.b = &ib;
    //s_test.c = &ic;

    cout << s_test.a << endl;
    cout << s_test.b << endl;
    cout << s_test.c << endl;

}