如何使Qt Creator在无返回语句上显示错误
How to make Qt Creator show error on no return statement
事实证明,g++编译器(默认情况下在Qt Creator中使用)给出一个简单的警告,如果你在非void函数中没有返回语句,即:
int* create_array(int n)
{
int* a = new int[n];
}
运行良好。
此行为在c++本身中受到无数错误报告的影响,但看起来开发人员认为此行为符合c++标准(这是有争议的,因为在这一部分中它有点令人困惑),如http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943:
所述:Flowing off the end of a function is equivalent to a return with no value;
this results in undefined behavior in a value-returning function.
然而,同样的段落以:
A return statement without an expression can be used only in functions
that do not return a value, that is, a function with the return type void,
a constructor (12.1), or a destructor (12.4).
所以除了这些(un)神圣的战争在标准的解释有任何选项,使Qt标志这是一个错误在编译时?
这与Qt无关,而是与g++有关。
在构建选项中,只需添加标志-Werror
, g++将把任何警告视为错误。您可能还需要使用标志-Wall
来让g++生成额外的警告,因为默认情况下,它不会为丢失的返回语句(以及许多其他情况)生成警告。但是,没有办法在每次警告的基础上调整此设置,因此要么全部设置,要么不设置。
您可以使用-Werror=warning_name
语法将特定警告视为错误。对于您的情况,您可以使用-Werror=return-type
(其中return-type
是"控制达到非无效函数结束"警告的名称)。根据https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
相关文章:
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 如何使C++调试语句显示在 gdb 控制台中
- if/else 语句输出由于加号或减号而未显示正确的消息
- 表单显示对话框函数错误,并且不执行下面的语句
- c++ IF 语句执行,但添加 ELSE 会显示错误
- 如果 return 语句在带括号的循环中,错误可能会到达非 void 函数的末尾,但不会显示在没有括号的循环的情况下
- 使用开关大小写语句抽取随机扑克牌等级和花色以显示用户
- 仅禁止显示实际的 make 命令和最终打印语句
- 如果/else语句未显示/可变分配问题的输出;初学者程序员
- 如果语句内部循环不读取完全正确的代表显示错误
- 如何使用我的 if-else 语句根据用户权重的输入执行特定操作,然后将其显示到控制台?
- 显示使用函数、switch 语句和多维数组的程序的多个不合逻辑的错误
- Switch语句不显示除默认语句之外的任何类型的输出
- 当我做出语句 root->right->right = newnode(7);注释,编译器显示运行时错误。谁能解释为什么?
- C++显示错误的 constexpr 错误语句
- 为什么我的输出没有显示"while"语句。while 语句用于显示所有信息
- 哪个工具可以在C++中显示方法或函数的最大语句
- C++-(If语句)总是显示Else
- Visual Studio在添加if语句后显示C++模糊性错误
- 从命令行启动时,在for循环期间不显示C++cout语句