警告 C4715 “MySinglelist<int>::pushDataAfter”: 不是所有的控件路径都返回值 链表list专栏 D:\C++learning\极客专栏数据结构与算法之美codes\链表list专栏\链表list专栏\MySinglelist.hpp 85
这其实就是编译器在提醒你:
MySinglelist<int>::pushDataAfter 这个函数你没有提供足够的return 语句,有可能你是在某个循环或者条件控制语句中return了,但是你在函数内的右括号 “}”结束时并没有加多一条return 语句,那么这就是不正确的!
请看以下代码:
MySinglelist.hpp:
#ifndef __MYSINGLELIST__
#define __MYSINGLELIST__
#include<iostream>
#include"SingleListNode.hpp"
using namespace std;
//实现没有头哨兵和尾哨兵的单链表的模板类
template<class T>
class MySinglelist
{
public:
MySinglelist()
{
//构造一个单链表
this->head = NULL;//维护头结点,head表示为头结点的意思,一开始先赋值为NULL,防止野指针的出现
this->Size = 0;
this->tempNode1 = NULL;
}
SingleListNode<T>* head;//维护头结点,head表示为头结点的意思
int Size;//维护单链表的长度size
SingleListNode<T>* tempNode1;// = new SingleListNode<T>(*head);
public:
//插入元素
//在data值为thisDataPosi的这个节点之后插入元素
bool pushDataAfter(T tdata);//, T thisDataPosi
//在data值为thisDataPosi的这个节点之前插入元素
bool pushDataBefore(T tdata);//, T thisDataPosi
//输出all元素
void printElem();
};
template<class T>
bool MySinglelist<T>::pushDataAfter(T tdata)//, T thisDataPosi
{
//先创建一个新的结点
//shared_ptr<SingleListNode<T>> newNode(new SingleListNode<T>);
SingleListNode<T>* newNode = new SingleListNode<T>;
newNode->data = tdata;
newNode->next = NULL;
if (tempNode1 == NULL && this->Size == 0) {//此时因为没有元素,因此需要你直接在head后插入元素
//用智能指针防止内存泄漏问题
//SingleListNode<T>* newNode = new SingleListNode<T>;//则code就要你手动释放堆区内存了
tempNode1 = newNode;
cout <<"head->data : "<< tempNode1->data << endl;
tempNode1->next = NULL;
this->Size++;
head = tempNode1;//这只会插入一次,所有让head指向tempNode1
return true;
}
else if (tempNode1->next == NULL) {//此时head是最后一个元素了,是从链表的最后去插入元素 这是easy job 啊
tempNode1->next = newNode;
tempNode1 = newNode;//你得往后移动!这一步非常关键!
this->Size++;
cout << "在尾部插入了!" << endl;
return true;
}
//这一条结束的return语句是非常重要的!不加就会导致编译器给你报错!
//return false;
}
template<class T>
bool MySinglelist<T>::pushDataBefore(T tdata)//, T thisDataPosi
{
return false;
}
template<class T>
//输出all元素
void MySinglelist<T>::printElem()
{
int i = 0;
cout << "this->Size : " << this->Size << endl;
SingleListNode<T>* tempNode = new SingleListNode<T> ;
tempNode = this->head;
while (i < this->Size)
{
cout << i << "轮:" << tempNode->data << "\t";
tempNode = tempNode->next;
i++;
}
cout << endl;
delete tempNode;//push完就delete它
}
#endif // __MYSINGLELIST__
SingleListNode.hpp:
#ifndef __SINGLELISTNODE__
#define __SINGLELISTNODE__
#include<iostream>
using namespace std;
template<class T>
class SingleListNode
{
public:
T data;//维护数据
SingleListNode<T> * next;//维护指向下一节点的指针next
public:
SingleListNode()
{
this->data = 0;
this->next = NULL;
}
};
#endif // !__SINGLELISTNODE__
main.cpp:
#include<iostream>
#include"MySinglelist.hpp"
#include"SingleListNode.hpp"
using namespace std;
void testMySingleList()
{
MySinglelist<int> mylist1;
bool ret1 = mylist1.pushDataAfter(2);
bool ret2 = mylist1.pushDataAfter(1);
bool ret3 = mylist1.pushDataAfter(9);
bool ret4 = mylist1.pushDataAfter(3);
bool ret5 = mylist1.pushDataAfter(2);
bool ret6 = mylist1.pushDataAfter(4);
bool ret7 = mylist1.pushDataAfter(5);
bool ret8 = mylist1.pushDataAfter(8);
mylist1.printElem();
}
int main()
{
testMySingleList();
system("pause");
return 0;
}
运行结果:
你会看到有“不是所有的控件路径都是返回值”这一警告warning提示,你要想尽一切办法debug所有的error和warning,这才是C++程序员该有的学习思想和态度。
但是当你把上面那行注释的 return false;取消注释之后,代码就可以正常运行了!
取消return false的注释后的运行结果: