[英]Omit return type in function prototype
from the C++ institute documentation ( an online course ):来自 C++ 学院文档(在线课程):
return_type describes the type of result returned (delivered) by the function (eg we expect that the sine function will return a value of type float as int data is completely unusable in this context);
return_type 描述了函数返回(传递)的结果类型(例如,我们期望正弦函数将返回一个 float 类型的值,因为 int 数据在这种情况下完全不可用); you can use any of the C++ types as a return_type, including a very special type named void;
您可以使用任何 C++ 类型作为 return_type,包括一个名为 void 的非常特殊的类型; a function of type void returns no result at all;
void 类型的函数根本不返回任何结果; we can say that such a function may have an effect but definitely has no result;
可以说,这样的函数可能有效果,但肯定没有结果; if you omit the return_type, the compiler assumes that the function returns a value of type int
如果省略 return_type,则编译器假定该函数返回 int 类型的值
regarding to this example return_type function_name (parameters_list);
关于这个例子
return_type function_name (parameters_list);
In this example:在这个例子中:
my_function(int x) {
return 4;
}
int main()
{
...
}
I get the following error: ISO C++ forbids declaration of 'my_function' with no type [-fpermissive]|
我收到以下错误:
ISO C++ forbids declaration of 'my_function' with no type [-fpermissive]|
In this example:在这个例子中:
my_function(int); //Prototype
int main()
{
...
}
int my_function(int x)
{
return 4;
}
I get the following error: expected constructor, destructor, or type conversion before ';' token
我收到以下错误:
expected constructor, destructor, or type conversion before ';' token
expected constructor, destructor, or type conversion before ';' token
I did not find in the C++11 standard page 192 - function declaration
something related to what i wanted to know (or maybe its just the fact that i did not understand).我在C++11 标准第 192 页中没有找到与我想知道的相关的
function declaration
(或者可能只是我不明白的事实)。
Could you please explain when can be omitted the return_type ?你能解释一下什么时候可以省略return_type吗? Is this a mistake?
这是一个错误吗? Or is some older version of C++?
或者是一些旧版本的C++?
Could you please explain when can be omitted the return_type?
你能解释一下什么时候可以省略 return_type 吗? Is this a mistake?
这是一个错误吗?
The return type may not be omitted in a regular function prototype.在常规函数原型中不能省略返回类型。 The resource you cited is very wrong to suggest otherwise.
你引用的资源是非常错误的建议否则。 There is no rule in standard C++ that assumes a return type of
int
in a function prototype.标准 C++ 中没有任何规则假定函数原型中的返回类型为
int
。
Or is some older version of C++?
或者是一些旧版本的C++?
Not of C++.不是 C++。 C++ never allowed omitting the return type.
C++ 不允许省略返回类型。 But pre-standardized C (K&R C) did allow it and had an "implicit int" rule.
但是预先标准化的 C (K&R C) 确实允许它并且有一个“隐式 int”规则。 And so some compilers offer an extension for compatibility with some really old C code.
因此,一些编译器提供了与一些非常旧的 C 代码兼容的扩展。
But again, this isn't, nor ever was, standard C++.但同样,这不是,也从来都不是,标准的 C++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.