筆者在調試c++的時候遇見了這個問題
E:\Data Struct\SqString\新建 文本文檔.cpp(5) : error C2258: illegal pure syntax, must be '= 0'
E:\Data Struct\SqString\新建 文本文檔.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 Struct\SqString\新建 文本文檔.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;
}
這樣是對的