很简单的小例子.报错.//主函数#include #include "list.h"using namespace std;int main(){\x05list11 mylist;\x05return 0;}
来源:学生作业帮助网 编辑:作业帮 时间:2024/08/10 18:49:03
很简单的小例子.报错.//主函数#include #include "list.h"using namespace std;int main(){\x05list11 mylist;\x05return 0;}
很简单的小例子.报错.
//主函数
#include
#include "list.h"
using namespace std;
int main()
{
\x05list11 mylist;
\x05return 0;
}
很简单的小例子.报错.//主函数#include #include "list.h"using namespace std;int main(){\x05list11 mylist;\x05return 0;}
问题不少啊:
1).h文件中,list11中把所有函数的实现即:{}部分都取消掉,因为.cpp文件中有实现了:
typedef struct node1 onenode;
typedef struct node1
{
int num;
onenode* next;
}*node;
class list11{
node head;
public:list11();
list11(const list11&rhs);
//list &operator=(const list&rhs){};
void insert(int i);
~list11();
void print(const list11&);
};
//list.c 改为list.cpp,因为using namespace std只能在C++中使用:
#include <iostream>
using namespace std;
#include "list.h" //把头文件放前面,切只能include一次
list11::list11(){head=NULL;} //分号位置错了
list11::list11(const list11&rhs)
{
int boolhead=1;
node rhscpy=rhs.head;
node headcpy=head;
while (rhscpy)
{
headcpy=(node)malloc(sizeof(onenode));//括号位置错了
if (boolhead==1)
{
boolhead=0;
head=headcpy;
}
headcpy->num=rhscpy->num;
headcpy->next=rhscpy->next;//这打错
rhscpy=rhscpy->next;
}
}
void list11:: insert(int i){
node newnode=(node)malloc(sizeof(onenode));
node headcpy=head;
while (headcpy)
{
headcpy=headcpy->next;
}
headcpy->next=newnode;
};
void list11::print(const list11&rhs)
{
node nodecpy=head;
while (nodecpy)
{
cout<<nodecpy->num<<endl;
nodecpy=nodecpy->next;
}
}
list11::list11()
{
while (head)
{
node headcpy=head->next;
free(head);
head=headcpy;
}
}
//主函数
int main()
{
list11 mylist;
return 0;
}