二进制表达式的无效操作数('ostream'(又名 'basic_ostream<char>')和 'void')

invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'void')

我在大学里有关于链表的数据结构作业,

执行了代码,但编译器 "eclips" 向我显示了一个名为 说明资源路径位置类型

二进制表达式的无效操作数('ostream'(又名 'basic_ostream')和 'void')LinkedLists.cpp /LinkedLists/src 第 156 行 C/C++问题

这是代码,这是什么问题?我该如何解决?

谢谢^^

struct node {
    int StudentNumber;
    string name;
    node *link,*data;

    };

node *head,*newNode,*last;
string name;
int StudentNumber;

node* insert (){
            char a='n';
            cout<<"hello to linked lists insertion"<<endl;

            cout<<endl<<"please write your name : ";
            cin>>name;
            cout<<"please put the student number : ";
            cin>>StudentNumber;
            head=NULL;

            while(a!='n'||a!='N'){

                newNode = new node ;
                newNode->StudentNumber=StudentNumber;
                newNode->name=name;
                newNode->link=NULL;

                if (head==NULL)
                {
                    head=newNode;
                    last=newNode;

                }else {

                    last->link=newNode;
                    last=newNode;

                }//end else


                cout<<"please write your name : ";
                cin>>name;
                cout<<endl<<"please put the student number : ";
                cin>>StudentNumber;
                cout<<"Do you want to insert new nodes ? y for yes , n for no ";
                cin>>a;


            }//end while

            return head;
    }//end insert function


// adding nodes function

void add() {
    int j;
    cout<<"please choose your option for adding new node : 1 for add at the beginning , 2 for add at the end ";
    cin>>j;
    if (head!=NULL)
        {
    switch (j)
{
    // adding at the beginning
case 1 :
        newNode=new node;
        newNode->link=head;
        head=newNode;
        cout<<"please insert your node data : ";
        cout<<"Student Name : "<<newNode->data<<endl<<"and student number is : "<<newNode->StudentNumber;

    break;
    // at the end
case 2:
    newNode= head;
    while (newNode->link!=NULL)
    {
        newNode = newNode->link;

    }// end of while
    last= newNode;
    newNode = new node ;
    newNode->link=NULL;
    last->link=newNode;
    cout<<"please insert your node data : ";
    cout<<"Student Name : "<<newNode->data<<endl<<"and student number is : "<<newNode->StudentNumber;
    break; // end of case adding at the end


}
}else {cout<<"The list is empty";}

}// ending of the adding nodes function


// delete node function

void deletion () {
    int s;
    cout<<"please choose your option for deleting nodes : 1 for delete the first , 2 for delete the last node ";
            cin>>s;
switch (s) {
// delete the first node
case 1 :
    newNode = head;
    last=head->link;
    head=last;
    delete newNode;
    break;

    //delete the last node
case 2:
    newNode=head;
    last=head;
    while (newNode->link!=NULL)
    {
        last=newNode;
        newNode=newNode->link;
    }
    last->link=NULL;
    delete newNode;
    break;


}//end of the switch


}// end of delete nodes function




int main() {
    int m;
    cout<<"Welcome to LinkedLists Example"<<endl;
    cout<<"enter your choice number , 1 for inserting nodes to the list , 2 for adding nodes , 3 for deleting nodes ";
    cin>>m;
    switch(m) {
    case 1:
        cout<<insert();
        break;
    case 2:
        cout<<add();
        break;
    case 3:
        cout<<deletion();
        break;


    }

return 0;

}

在您的 main 函数中,您有以下三行:

cout<<insert();
cout<<add();
cout<<deletion();

如果您查看这些函数的原型:

node* insert ()
void add() 
void deletion()

现在我们可以看到问题出在哪里了。在 insert 的情况下,您试图将 node* 传递给 coutcout 不知道如何处理 node*。同样,adddeletionvoid 传递给 coutcout 不知道如何处理。

有两种解决方案:

  1. insertadddelete中打包一个string,然后return那个字符串到cout
  2. 从这些行中删除 cout

我建议第二种解决方案。这对您的项目来说要简单得多,而且它不会破坏您计划使用 insert 的 return 值进行的任何操作。您的主要功能将如下所示:

int main() {
    int m;
    cout<<"Welcome to LinkedLists Example"<<endl;
    cout<<"enter your choice number , 1 for inserting nodes to the list , 2 for adding nodes , 3 for deleting nodes ";
    cin>>m;
    switch(m) {
      case 1:
        insert();
        break;
      case 2:
        add();
        break;
      case 3:
        deletion();
        break;
    }
    return 0;
}