07.11.2011, 09:46. Просмотров 791. Ответов 12
Пытаюсь сделать класс список и не получается написать конструктор копирования.
header
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| //
#include <iostream>;
using namespace std;
template <class T> class LIST
{
public:
class Node
{
public:
T data;
Node * next;
Node (T d=0)
{
data=d;
next=0;
}
};
Node * head;
public:
LIST (){head=0;}
~LIST ();
void insert_beg (T);
void insert_end (T);
void del (T);
T& find(T);
void display();
LIST (const LIST<T> list1);
}; |
|
cpp
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
| # include "LIST.h"
template <class T>
LIST <T>::LIST(const LIST<T> list1)
{
Node *t1,*t2;
t1=this->head;
t2=list1.head;
if(t2!=NULL)
{
t1=new Node;
t1->data=t2->data;
t2 = t2->next;
}
this->head = t1;
while (t2 != NULL)
{
this->insert_end(t2->data);
t2 = t2->next;
}
t2 = list1.head;
}
template <class T>
void LIST <T>::insert_beg (T dat)
{
Node * nel=new Node(dat);
nel->next=head;
head=nel;
}
template <class T>
LIST <T>::~LIST ()
{
Node *p = head, *a;
while (p != 0)
{ a = p;
p = p->next;
delete a;
}
head = 0;
}
template <class T>
void LIST<T>::insert_end (T dat)
{
Node *temp=new Node(dat);
Node *p = head;
Node *a = 0;
while (p !=0)
{
a = p;
p = p->next;
}
a->next = temp;
temp->next = 0;
}
template <class T>
void LIST<T>::display()
{
Node *p = head;
while (p != 0)
{
cout<<p->data;
p = p->next;
}
cout<<endl;
}
template <class T>
T& LIST<T>::find(T dat)
{
Node *p = head, *a = 0;
while (p != 0)
{
if ((p->data) == dat)
{
a = p;
break;
}
p = p->next;
}
if (a == 0)
{
cout<<dat;
cout<<"not found"<<endl;
// void &r = NULL;
// return 0;
}
else
{
int &r_a = (a->data);
return r_a;
}
}
template <class T>
void LIST<T>::del (T dat)
{
Node *p = head, *a = 0;
while (p != 0)
{
a = p;
p = p->next;
if ((p->data) == dat)
{
break;
}
}
a->next = a->next->next;
}
int main()
{
LIST<int> qq;
qq.insert_beg(7);
qq.insert_beg(3);
qq.insert_beg(8);
qq.insert_end(4);
qq.insert_end(5);
LIST<int> ww (qq);
ww.display();
qq.display();
qq.find(3) = 9;
qq.display();
qq.del(7);
qq.display();
} |
|
Выводит такую ошибку: error C2652: 'LIST<T>' : illegal copy constructor: first parameter must not be a 'LIST<T>'
1> c:\users\я\documents\visual studio 2008\projects\list\list\list.h(29) : see reference to class template instantiation 'LIST<T>' being compiled
Пробовал делать конструктор от двух параметров, но тогда появляются другие проблемы.
Объясните плз, что и где поменять.