在调试C++程序是出现这个问题的解决方案illegal pure syntax, must be '= 0'

笔者在调试c++的时候遇见了这个问题

E:Data StructSqString新建 文本文档.cpp(5) : error C2258: illegal pure syntax, must be '= 0'
E:Data StructSqString新建 文本文档.cpp(5) : error C2252: 'length' : pure specifier can only be specified for functions

代码如下:

#include<stdio.h>
#include<malloc.h>
typedef struct array
{
int length=50;

}SqString;
void main()
{
SqString st;
}

2、根据第一个错误,我们把length值设为0

出现

E:Data StructSqString新建 文本文档.cpp(5) : error C2252: 'length' : pure specifier can only be specified for functions

MSDN提示错误C2252如下:'identifier' : pure specifier can only be specified for functions

《C++编程思想》解释如下:不能这样初始化。因为定义结构体时,并未给其分配内存,所以初值是无法存储的。

应该声明结构体变量后,手工赋值在结构体和类中不能对成员初始化。

#include<stdio.h>
#include<malloc.h>
typedef struct array
{
int length;

}SqString;
void main()
{
SqString st;
st.length=10;
}

这样是对的

原文地址:https://www.cnblogs.com/kwdeblog/p/5238104.html