C ++函数原型? [英] C++ function prototypes?

查看:161
本文介绍了C ++函数原型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是新手问题5,但我没有老师..所以..无论如何,我们去:

Here goes newbie question number 5, but I don't have a teacher.. so.. anyhow here we go:

我想知道如果是必要的在文件的顶部有函数原型,而不是将 main 函数放到文件的末尾,并在文件的顶部创建所有的函数。据我所知,VC ++和G ++都不是抱怨。是否有不允许我这样做的标准?

I'm wondering if is necessary to have function prototypes at the top of the file, instead of putting the main function to the end of the file and create all the function at the top of the file. As far as I can tell, VC++ and G++ both are not complaining. Is there standards that disallows me to do so?

更改函数参数和返回类型时,更改原型似乎很烦人。

It seems rather annoying to have to change the prototype when you change a function parameters and return types.

示例:

#include <iostream>

void say_hi(){
    std::cout << "hi" << std::endl;
}

int main(){
    say_hi();
    return 0;
}


推荐答案

函数 say_hi

void say_hi();

这两个都声明并定义了 say_hi

This both declares and defines the function say_hi:

void say_hi(){
    std::cout << "hi" << std::endl;
}

您可以多次声明函数;您只能定义一次。

You can declare a function many times; you can only define it once.

必须先在文件之前声明函数,然后才能调用它。

A function must be declared in the file before you can call it. A function must be defined somewhere--in the same file before or after you call it or maybe even in a different file.

因此,一个函数必须定义在某处 - 在同一个文件中,在调用它之前或之后,是的,这是完全正常。

So, yes, this is perfectly fine.

这篇关于C ++函数原型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆