error C2648: “MyDoublyLinkedlist<int>::length”: 将成员作为默认参数使用要求静态成员

来源:互联网 发布:软件开发设计文档 编辑:程序博客网 时间:2024/05/30 04:30

C++中只能将静态成员设置为默认参数,默认参数不能是一个普通变量。


代码如下(insert函数有误):

#pragma once#include<iostream>template<typename Element> class MyDoublyLinkedlistNode;template<typename Element>class MyDoublyLinkedlist;template<typename Element>class MyDoublyLinkedlistNode{template<typename X> friend class MyDoublyLinkedlist;public:MyDoublyLinkedlistNode(Element nodeValue) :nodeValue(nodeValue){}private:Element nodeValue;MyDoublyLinkedlistNode<Element>* prev = nullptr,* next = nullptr;};template<typename Element> class MyDoublyLinkedlist{public:MyDoublyLinkedlist():header(nullptr){};MyDoublyLinkedlist(Element firstNodeValue) :header(new MyDoublyLinkedlistNode<Element>(firstNodeValue)),length(1){}bool isEmpty(){ return header == nullptr; }MyDoublyLinkedlistNode<Element>* searchByValue(Element searchValue){MyDoublyLinkedlistNode<Element>* tmpPoint = header;while (tmpPoint != nullptr){if (tmpPoint->nodeValue == searchValue)break;elsetmpPoint = tmpPoint->next;}return tmpPoint;}MyDoublyLinkedlistNode<Element>* insert(Element insertValue, size_t index=length)//insert back {if (index > length)index = length;MyDoublyLinkedlistNode<Element>* tmpPoint = header;while (index != 0){tmpPoint = tmpPoint->next;}MyDoublyLinkedlistNode<Element>* trans = tmpPoint->next;tmpPoint->next = new MyDoublyLinkedlistNode<Element>(insertValue);tmpPoint->next->next = trans;if (trans != nullptr){trans->prev = tmpPoint->next;}if (tmpPoint != header){tmpPoint->next->prev = tmpPoint;}++length;}void print(){MyDoublyLinkedlistNode<Element>* tmpPoint = header;while (!(tmpPoint == nullptr)){std::cout << tmpPoint->nodeValue << " ";tmpPoint = tmpPoint->next;}std::cout << std::endl;}private:MyDoublyLinkedlistNode<Element>* header = nullptr;size_t length = 0;};

原创粉丝点击