我收到警告:控制达到非无效功能的末尾

  • 本文关键字:功能 无效 警告 控制 c++
  • 更新时间 :
  • 英文 :


我的程序似乎在visual studio中工作得很好,但当我在GCC中运行它时,它给了我一个名为

的编译错误
Book.cpp: In member function ‘sdds::Book& sdds::Book::addChapter(const char*, int)’:
Book.cpp:64:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^

我不知道该怎么办,有人能帮帮我吗?这就是我在

中得到错误的函数
Book& addChapter(const char* chapter_name, int noOfPages)
{
// create a new chapter
Chapter chapter(chapter_name, noOfPages);
// check if chapter is valid
if (chapter.isValidChapter()) {
// add the chapter to list of chapters
// create a new list of chapters
Chapter* chapters = new Chapter[this->noOfChapters + 1];
// copy chapters to new array
for (int i = 0; i < this->noOfChapters; i++)
chapters[i] = this->chapters[i];
// add the last chapter
chapters[this->noOfChapters++] = chapter;
// delete the old list of chapters
delete this->chapters;
// update the list of chapters
this->chapters = chapters;
return *this;
}
}

该警告表示函数:addChapter等待某个返回值。
当然,您在If statment中有一个返回值,但是,如果语句为False,程序可能会崩溃。因为函数不需要返回任何东西,但是期望返回一些东西。
简单修复:确保函数总是返回一些值

最新更新