简体   繁体   English

链表c ++,预期主表达式在->之前

[英]Linked list c++ , expected primary expression before ->

im trying to learn linked lists in c++; 我试图学习C ++中的链表; my code look like this 我的代码看起来像这样

#include <iostream>
#include <string>

using namespace std;
struct node{

    int number;
    node *next;

};

struct zoznam{

    node *head=NULL;
    node *tail=NULL;

};

void insertAsFirst(node *&head,node *&last, int number){

    node *tmp = new node;
    tmp->number = number;
    tmp->next=NULL;
    head=tmp;
    last=tmp;

}
void insertValues( node *&head , node *&last, int number){

    if(head==NULL){
        insertAsFirst(zoznam->head,zoznam->tail,number);
    }else{

        node *tmp = new node;
        tmp->number=number;
        tmp->next=NULL;
        last->next=tmp;
        //last=tmp;


    }
}
int main()
{
    for( int i = 0; i < 10; i++){
        insertValues(zoznam->head,zoznam->tail,i);
    }
    node *current=zoznam->head;
    while(current!=NULL){
        cin<<current->number << endl;
        current=current->next;
    }
    return 0;
}

expected primary-expression before '->' token| '->'标记之前的预期主要表达式|

Being new to c++ , i have no idea what the error means , how can i fix that , i tried to check it up but found nothing. 对C ++来说,我是一个陌生的人,我不知道该错误意味着什么,如何解决该问题,我尝试对其进行检查但未发现任何错误。 Thanks 谢谢

As you're new to C++, I am giving you some details. 由于您是C ++的新手,所以我为您提供一些详细信息。

When you are using struct I mean you define any struct then it becomes a type name not a variable. 当您使用struct我的意思是您定义任何struct然后它将成为类型名称而不是变量。 To use that you need to declare variable(s) of that type. 要使用它,您需要声明该类型的变量。

Here, in the main , you're using zoznam , which is a type name and the definition of that type is the definition of the struct . 在这里, main是使用zoznam ,这是一个类型名称,该类型的定义是struct的定义。

Declaration: zoznam var_name; 声明: zoznam var_name;

Use: var_name.head; 使用: var_name.head; or ' var_name.tail ;'. 或' var_name.tail ;'。

Learn more about struct and pointer . 了解有关结构指针的更多信息。 Read the documentations carefully not only the example codes in this two link. 不仅在这两个链接中的示例代码,还要仔细阅读文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM