编译琐事:错误''没有命名类型

Compilation trivia : error ' ... ' does not name a type

本文关键字:类型 错误 编译      更新时间:2023-10-16

我知道这个问题以前在这里被问过。但我就是做不好。(可能就是其中一次!(。无论如何,这里是一个简单的代码的链接列表

#include <iostream>
typedef struct nodeType {
               int data;
               struct nodeType *next;
         }node;
typedef node *list;
list temp = new nodeType();
temp->data = 5;

我无法编译这段简单的代码!

g++ -g -c list.cpp
error: ‘temp’ does not name a type
typedef struct nodeType {
               int data;
               struct nodeType *next;
         }node;

在C++中没有必要,因为再也不需要复杂的类型说明符了。

struct nodeType 
{
     int data;
     nodeType *next;
};

当然,必须调整相应的CCD_ 1。


list temp = new nodeType();
temp->data = 5;

所有语句都必须进入像main这样的函数内部,然后应该delete节点;

int main()
{
    list temp = new nodeType; // braces not necessary
    temp->data = 5;
    delete temp;
}

或者直接将其放入智能指针中。或者更好:编写一个使用RAII创建和销毁节点的类。基本上是CCD_ 4。