我已经很久没有用 C 编写代码了,现在我什至无法创建链表:( NodeType
结构可能有什么问题?
我什至试过this例如,我仍然收到与此类似的错误。
我需要创建可以在 linux 和 windows 上运行的链表(无需大量修改)。
我使用:cl myFile.c
命令进行编译。
错误信息:
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.40219.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.
unlock.c unlock.c(46) : error C2275: 'Node' : illegal use of this type as an expression unlock.c(17) : see declaration of 'Node' unlock.c(46) : error C2146: syntax error : missing ';' before identifier 'a' unlock.c(46) : error C2065: 'a' : undeclared identifier
源代码:
#include <stdio.h>
#include <windows.h>
#include<stdlib.h>
typedef enum {STABLE, RPUSH, LPUSH} STATUS_TYPE;
typedef struct NodeType
{
struct NodeType* _left;
struct NodeType* _right;
int _value;
}Node;
typedef struct AnchorType
{
struct Node* _mostLeft;
struct Node* _mostRight;
STATUS_TYPE _status;
} Anchor;
Node CreateNode(int data)
{
Node newNode;
newNode._value = data;
newNode._left = NULL;
newNode._right = NULL;
return newNode;
}
int main()
{
Anchor anchor;
anchor._mostLeft = NULL;
anchor._mostRight = NULL;
anchor._status = STABLE;
Node a; //<-- What might be wrong ?
return 0;
}
感谢您的帮助。
最佳答案
Microsoft 专注于 C++ 编译器,他们对 C 的支持已经过时了几十年。
Microsoft 编译器不支持的较新 C 标准的功能之一是能够在函数中间声明变量。
把声明移到最上面,一切就ok了:
int main()
{
Anchor anchor;
Node a; // ok here
anchor._mostLeft = NULL;
anchor._mostRight = NULL;
anchor._status = STABLE;
//Node a; but not here
return 0;
}
关于c - C Windows 中的简单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12608095/