很简单的小例子。报错。

2025-04-17 00:31:45
推荐回答(1个)
回答(1):

改了下你看看

///////////////////////////////////////////////////////////////   newfile.h

 * File:   newfile.h

 * Author: chujiangke

 *

 * Created on 2013年9月2日, 下午12:50

 */


#ifndef NEWFILE_H

#define NEWFILE_H



#include

#include

#include

using namespace std;


typedef struct node1

{

int num;

node1* next;

}*node;


class list11{

  node head;

public:

      list11();

 list11(const list11&rhs);

 //list &operator=(const list&rhs){};

 void insert(int i);

 ~list11();

 void print();

};

list11::list11(){head=NULL;};

list11::list11(const list11&rhs)

{

int boolhead=1;

node rhscpy=rhs.head;

node headcpy;

while (rhscpy)

{

headcpy=(node)malloc(sizeof(node));

if (boolhead==1)

{

boolhead=0;

head=headcpy;

headcpy->num=rhscpy->num;

rhscpy=rhscpy->next;

boolhead++;

}

else

{

 

 headcpy->num=rhscpy->num;

                  headcpy->next=head->next;

                  head->next=headcpy;

 rhscpy=rhscpy->next;


}


}


}


void list11:: insert(int i){

    if(head==NULL)

    {

        head=(node)malloc(sizeof(node));

        head->num=i;

    }

    else{

        node temp=(node)malloc(sizeof(node));

        temp->num=i;

        

        node point=head;

        while(point->next!=NULL)

        {

            point=point->next;

        }

        point->next=temp;

    }


};


void list11::print()

{

node nodecpy=head;

while (nodecpy)

{

cout<num<

nodecpy=nodecpy->next;

}

}


 list11::~list11()

{


while (head)

{

node headcpy=head->next;

free(head);

head=headcpy;


}

}



#endif /* NEWFILE_H */



//////////////////////////////////////////////////  main.cpp

#include

#include "newfile.h"

using namespace std;


int main()

{

    cout << "Hello world!" << endl;

    list11 mylist;

    mylist.insert(1);

    mylist.insert(2);

    mylist.insert(3);

    mylist.print();


    return 0;

}