函数原型和函数

来源:互联网 发布:软件需求变更 编辑:程序博客网 时间:2024/05/22 16:59

他之所以不编译这个程序是因为编译器会按顺序读取文件。当编译器为函数的调用()对main()线6,不知道什么补充的是,因为我们还没有定义()到10号线!产生第一个错误(“未找到的标识符”)。

zhīsuǒbiānzhègechéngshìyīnwèibiānhuìànshùnwénjiàndāngbiānwéihánshùdediàoyòngduìmain()xiàn线6zhīdaoshénmechōngdeshìyīnwèimenháiméiyǒudìngdào10hàoxiàn线chǎnshēngcuòwèizhǎodàodebiāoshí

When Visual Studio 2005 gets to the actual declaration of add() on line 10, it also complains about add being redefined. This is somewhat misleading, given that it wasn’t ever defined in the first place. Later versions of Visual Studio correctly omit this additional error message.

当Visual Studio 2005到10线()实际的宣言,也抱怨将被重新定义。这是有点误导,因为它没有被定义在第一个地方。后来版本的视觉工作室正确省略此附加错误信息。

dāngVisualStudio2005dào10xiàn线shídexuānyánbàoyuànjiāngbèichóngxīndìngzhèshìyǒudiǎndǎoyīnwèiméiyǒubèidìngzàifānghòuláibǎnběndeshìjuégōngzuòshìzhèngquèshěnglvèjiācuòxìn

Despite the redundancy of the second error, it’s useful to note that it is fairly common for a single error to produce (often redundant) multiple compiler errors or warnings.

尽管二次错误的冗余,它是有用的注意,它是相当常见的一个单一的错误,产生(通常是多余的)多个编译器错误或警告。

jǐnguǎnèrcuòderǒngshìyǒuyòngdezhùshìxiāngdāngchángjiàndedāndecuòchǎnshēngtōngchángshìduōdeduōbiāncuòhuòjǐnggào

Rule: When addressing compile errors in your programs, always resolve the first error produced first.

规则:在处理程序中编译错误时,始终先解决第一个错误。

guīzàichǔchéngzhōngbiāncuòshíshǐzhōngxiānjiějuécuò

To fix this problem, we need to address the fact that the compiler doesn’t know what add is. There are two common ways to address the issue.

要解决这个问题,我们需要解决编译器不知道该添加的是什么。解决这个问题有一个共同的方法。

yàojiějuézhègewènmenyàojiějuébiānzhīdaogāitiānjiādeshìshénmejiějuézhègewènyǒugòngtóngdefāng

Option 1: Reorder the function calls so add() is defined before main():

选项1:排序函数调用,所以()定义在main():

#include <iostream> int add(int x, int y){    return x + y;} int main(){    using namespace std;    cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;    return 0;}

这样,通过时间main()()调用,编译器会知道什么是()。因为这是这样一个简单的程序,这种变化比较容易。然而,在一个更大的程序,它可以是乏味的,试图找出哪些函数调用的其他功能(以及在什么顺序),使它们可以被声明为顺序。

zhèyàngtōngguòshíjiānmain()diàoyòngbiānhuìzhīdaoshénmeshìyīnwèizhèshìzhèyàngjiǎndāndechéngzhèzhǒngbiànhuàjiàoróngránérzàigèngdechéngshìwèideshìzhǎochūxiēhánshùdiàoyòngdegōngnéngzàishénmeshùnshǐ使menbèishēngmíngwéishùn

Furthermore, this option is not always possible. Let’s say we’re writing a program that has two functions A and B. If function A calls function B, and function B calls function A, then there’s no way to order the functions in a way that they will both be happy. If you define A first, the compiler will complain it doesn’t know what B is. If you define B first, the compiler will complain that it doesn’t know what A is.

此外,这一选择是不可能的。让我们说,我们正在编写一个程序,它具有2个功能,一个和B,如果函数调用函数,和函数调用函数,那么有没有办法,以一种方式,他们将是幸福的方式。如果你定义了第一个,编译器会抱怨它不知道是什么。如果你先定义了一个,编译器会抱怨它不知道是什么。

wàizhèxuǎnshìnéngderàngmenshuōmenzhèngzàibiānxiěchéngyǒu2gōngnéngBguǒhánshùdiàoyònghánshùhánshùdiàoyònghánshùmeyǒuméiyǒubànzhǒngfāngshìmenjiāngshìxìngdefāngshìguǒdìnglebiānhuìbàoyuànzhīdaoshìshénmeguǒxiāndìnglebiānhuìbàoyuànzhīdaoshìshénme

Function prototypes and forward declaration of functions

函数原型和函数的前向声明

hánshùyuánxínghánshùdeqiánxiàngshēngmíng

Option 2: Use a forward declaration.

选项2:使用前向声明。

xuǎnxiàng2shǐ使yòngqiánxiàngshēngmíng

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier.

一个向前声明允许我们在实际上定义标识符之前先向编译器说明一个标识符的存在。

xiàngqiánshēngmíngyǔnmenzàishíshangdìngbiāoshízhīqiánxiānxiàngbiānshuōmíngbiāoshídecúnzài

In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function’s body. This way, when the compiler encounters a call to the function, it’ll understand that we’re making a function call, and can check to ensure we’re calling the function correctly, even if it doesn’t yet know how or where the function is defined.

在功能的情况下,这使我们能够告诉编译器的功能的存在之前,我们定义的功能的身体。当编译器遇到一个函数调用时,它会理解我们正在进行一个函数调用,并且可以检查以确保我们调用该函数,即使它还不知道如何定义该函数。

zàigōngnéngdeqíngkuàngxiàzhèshǐ使mennénggòugàobiāndegōngnéngdecúnzàizhīqiánmendìngdegōngnéngdeshēndāngbiāndàohánshùtiáoyòngshíhuìjiěmenzhèngzàijìnxínghánshùdiàoyòngbìngqiějiǎncháquèbǎomendiàoyònggāihánshùshǐ使háizhīdaodìnggāihánshù

To write a forward declaration for a function, we use a declaration statement called a function prototype. The function prototype consists of the function’s return type, name, parameters, but no function body (the part between the curly braces). And because the function prototype is a statement, it ends with a semicolon.

要编写一个函数的前向声明,我们使用声明语句称为函数原型。函数原型包括函数的返回类型、名称、参数,但没有任何函数体(大括号之间的部分)。因为函数原型声明,它以分号结束。

yàobiānxiěhánshùdeqiánxiàngshēngmíngmenshǐ使yòngshēngmíngchēngwéihánshùyuánxínghánshùyuánxíngbāokuòhánshùdefǎnhuílèixíngmíngchēngcānshùdànméiyǒurènhánshùkuòhàozhījiāndefenyīnwèihánshùyuánxíngshēngmíngfēnhàojiéshù

Here’s a function prototype for the add() function:

这是一本()函数的函数原型:

123456789101112131415#include <iostream> int add(int x, int y); // forward declaration of add() (using a function prototype) int main(){    using namespace std;    cout << "The sum of 3 and 4 is: " << add(3, 4) << endl; // this works because we forward declared add() above    return 0;} int add(int x, int y) // even though the body of add() isn't defined until here{    return x + y;


0 0
原创粉丝点击