#include<bits stdc++.h>
using namespace std;
const int MaxSize = 10;
template <class t>
class Seqlist {
public:
Seqlist() { length = 0;}
Seqlist(T [],int);
private:
int length;
T data[MaxSize];
};
class B {
public:
B(T);
B() {} //这个默认的构造函数十分重要
private:
T data;
};
B::B(T n) {
data = n;
}
template <class t>
Seqlist<t>::Seqlist(T arr[], int n) {
if (n > MaxSize) throw "invalid num";
for (int i = 0;i < n;i++) {
data[i] = arr[i];
}
length = n;
}
int main() {
B b[3] = {B(10), B(20), B(30)};
Seqlist<b> list(b,3);
}
</b></t></class></class></bits>
在上面部分代码中,如果把有注释的那一行代码删去,则这部分代码不能正常运行,报错信息是
no matching function for call to 'B::B()'[25, 35]
根据这个报错信息找到相应行数,居然在 int n
这里报错,我寻思着这关n什么事,后面查阅了资料,原因出在第34行的 B b[3]
这里,定义了一个B类型的数组,没有参数传入,而B类中无适配该成员的构造函数,于是编译器报错
解决方法是,加上相应的构造函数 B() {}
#include<bits stdc++.h>
using namespace std;
template <class t>
struct Node {
T data;
Node<t>* next;
};
template <class t>
class LinkList {
public:
LinkList(); // create an empty list
LinkList(T[], int);
//~LinkList();
void Reverse(Node<t>* head);
Node<t>* Copy(Node<t>* head);
Node<t>* first;
}
template <class t>
LinkList<t>::LinkList() {
first = new Node<t>;
first->next = NULL;
}
template <class t>
LinkList<t>::LinkList(T arr[], int n) {
first = new Node<t>;
Node<t>* p = first;
for (int i = 0;i < n;i++) {
Node<t>* s = new Node<t>;
s->data = arr[i];
p->next = s;//让first指向第一个节点
p = s;//虽说把s的地址赋给p,但是first的地址和p不一样,对p操作不会改变first
//因此first永远指向链表第一个位置
}
p->next = NULL;
}
template <class t>
void LinkList<t>::Reverse(Node<t>* head) {
Node<t>* pre, *cur;
cur = head->next;
head->next = NULL;
while (cur) {
pre = cur;//让当前节点的上一节点移动到当前节点位置
cur = cur->next;//当前节点移动到下一个位置
pre->next = head->next;//这一操作就是让pre不断指向前一个节点
head->next = pre;//更新节点,让head的next指向pre
}
//在画图理解时,要让cur和pre同时向后移动
//这一部分,建议自己在纸上画出若干个节点和用三个小部件(代表三个指针)模拟一边才能够真正地理解
}
//复盘时自己有个小细节没注意到,这里都是指针变量的赋值操作,这里的赋值只是赋予地址,给予者的值改变,接收者的值不会改变
template <class t>
Node<t>* LinkList<t>::Copy(Node<t>* head) {
Node<t>* headB = new Node<t>;
Node<t>* pb = headB;
Node<t>* pa = head->next;
while (pa != NULL) {
Node<t>* temp = new Node<t>;
temp->data = pa->data;
temp->next = NULL;//这里用到了一个临时指针变量
pb->next = temp;//让pb连上temp
pb = pb->next;
pa = pa->next;
//这里向后移动
}
return headB;
//注意这里返回的是headB,在调用时记得再加一个"->next"
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
LinkList<int> list(arr,5);
cout << "the origin state: " << endl;
list.PrintList();
cout << endl << "copy list a to list b\n";
Node<int>* ptr = new Node<int>;
ptr = list.copy(list.first)->next;
while (ptr != NULL) {
cout << ptr->data << ' ';
ptr = ptr->next;
}
cout << endl << "reverse the list a\n";
list.Reverse(list.first);
list.PrintList();
}
</int></int></int></t></t></t></t></t></t></t></t></t></class></t></t></t></class></t></t></t></t></t></class></t></t></class></t></t></t></t></class></t></class></bits>
Original: https://www.cnblogs.com/jaydenchang/p/15450265.html
Author: JaydenChang
Title: 数据结构(C++)–学习单链表时发现的一些小坑
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/585164/
转载文章受原作者版权保护。转载请注明原作者出处!