函数原型和函数

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

zhī suǒ biān zhè ge chéng shì yīn wèi biān huì àn shùn wén jiàn dāng biān wéi hán shù de diào yòng duì m a i n ( ) xiàn线 6 zhī dao shén me chōng de shì yīn wèi men hái méi yǒu dìng dào 1 0 hào xiàn线 chǎn shēng cuò wèi zhǎo dào de biāo shí

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āng V i s u a l S t u d i o 2 0 0 5 dào 1 0 xiàn线 shí de xuān yán bào yuàn jiāng bèi chóng xīn dìng zhè shì yǒu diǎn dǎo yīn wèi méi yǒu bèi dìng zài fāng hòu lái bǎn běn de shì jué gōng zuò shì zhèng què shěng lvè 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ǐn guǎn èr cuò de rǒng shì yǒu yòng de zhù shì xiāng dāng cháng jiàn de dān de cuò chǎn shēng tōng cháng shì duō de duō biān cuò huò jǐng gào

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

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

guī zài chǔ chéng zhōng biān cuò shí shǐ zhōng xiān jiě 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ào jiě jué zhè ge wèn men yào jiě jué biān zhī dao gāi tiān jiā de shì shén me jiě jué zhè ge wèn yǒu gòng tóng de fā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àng tōng guò shí jiān m a i n ( ) diào yòng biān huì zhī dao shén me shì yīn wèi zhè shì zhè yàng jiǎn dān de chéng zhè zhǒng biàn huà jiào róng rán ér zài gèng de chéng shì wèi de shì zhǎo chū xiē hán shù diào yòng de gōng néng zài shén me shùn shǐ使 men bèi shēng míng wéi shù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ài zhè xuǎn shì néng de ràng men shuō men zhèng zài biān xiě chéng yǒu 2 gōng néng B guǒ hán shù diào yòng hán shù hán shù diào yòng hán shù me yǒu méi yǒu bàn zhǒng fāng shì men jiāng shì xìng de fāng shì guǒ dìng le biān huì bào yuàn zhī dao shì shén me guǒ xiān dìng le biān huì bào yuàn zhī dao shì shén me

Function prototypes and forward declaration of functions

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

hán shù yuán xíng hán shù de qián xiàng shēng míng

Option 2: Use a forward declaration.

选项2:使用前向声明。

xuǎn xiàng 2 shǐ使 yòng qián xiàng shēng míng

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

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

xiàng qián shēng míng yǔn men zài shí shang dìng biāo shí zhī qián xiān xiàng biān shuō míng biāo shí de cún zà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ài gōng néng de qíng kuàng xià zhè shǐ使 men néng gòu gào biān de gōng néng de cún zài zhī qián men dìng de gōng néng de shēn dāng biān dào hán shù tiáo yòng shí huì jiě men zhèng zài jìn xíng hán shù diào yòng bìng qiě jiǎn chá què bǎo men diào yòng gāi hán shù shǐ使 hái zhī dao dìng gāi hán shù

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ào biān xiě hán shù de qián xiàng shēng míng men shǐ使 yòng shēng míng chēng wéi hán shù yuán xíng hán shù yuán xíng bāo kuò hán shù de fǎn huí lèi xíng míng chēng cān shù dàn méi yǒu rèn hán shù kuò hào zhī jiān de fen yīn wèi hán shù yuán xíng shēng míng fēn hào jié shù

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值