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]
}
^
我不知道该怎么做,有人可以帮忙告诉我应该改变什么吗?这是我收到错误的文件。函数是第一章的add。我使用的返回语句是“return *this”,但这似乎不起作用。我的代码在 Visual Studio 中编译得很好,但在 GCC 中编译得不好。
namespace sdds
{
Book::Book(const char* book_name, int noOfChapters, const Chapter* chapters)
{
// initially assume that everything is valid
this->isValid = true;
this->book_name = nullptr;
this->chapters = nullptr;
this->noOfChapters = 0;
int length = (book_name != nullptr ? strlen(book_name) : 0);
// check if book_name is valid
if (length < 1) this->isValid = false;
if (noOfChapters < 1) this->isValid = false;
// check if all chapters are valid
for (int i = 0; i < noOfChapters; i++)
if (!chapters[i].isValidChapter())
this->isValid = false;
// check if valid information is provided to create book
if (this->isValid) {
// create a string on heap for book_name
this->book_name = new char[length + 1];
//copy the elements
strcpy(this->book_name, book_name);
// allocate memory for chapters
this->chapters = new Chapter[noOfChapters];
// create copies of chapters
for (int i = 0; i < noOfChapters; i++)
this->chapters[this->noOfChapters++] = chapters[i];
}
}
bool Book::isEmpty() const
{
// check if book is empty or invalid
return (isValid || this->noOfChapters == 0);
}
Book& 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;
}
}
void Book::display() const
{
// check if book is valid3
if (!isValid) {
std::cout << "Invalid Book object";
std::cout << std::endl;
}
else {
std::cout << "Book Name: " << this->book_name << std::endl;
std::cout << "No of Chapters: " << this->noOfChapters << std::endl;
std::cout.width(50);
std::cout << std::left;
std::cout.fill(' ');
std::cout << "Chapter name" << " ";
std::cout << "Pages";
std::cout << std::endl;
for (int i = 0; i < noOfChapters; i++) {
// display the ith chapter
chapters[i].display();
std::cout << std::endl;
}
}
}
Book::~Book()
{
// delete memory allocaed to book_name and chapters
if (book_name != nullptr) delete book_name;
if (chapters != nullptr) delete chapters;
}
}
解决方案
推荐阅读
- javascript - 使用 ajax xmlhttprequest 对象后,javascript 和 css 文件不起作用
- typo3 - TYPO3 中博客文章列表中显示的元素
- java - Mybatis——QueryBuilder类中的参数映射
- testing - java.net.SocketException:权限被拒绝:连接,这是什么原因?
- ios - 每次启动我的应用程序时都会出现位置图标
- firebase - `firebase.functions().httpsCallable` 如何将文件提交到`functions.https.onCall`?
- python - 为什么这两种构建矩阵的方式不同
- python - 使用 django 在 sqllite 上的 blob 文件
- c++ - 在 wxWebView 中弹出句柄身份验证
- jquery - 如何在 Firefox 中隐藏滚动条而不停止在 div 中滚动