不能解决函数调用!

然而,我们更喜欢我们的参数,因为它允许您了解什么功能参数只是通过看原型。否则,你必须找到实际的函数定义。

rán ér men gèng huan men de cān shù yīn wèi yǔn nín liǎo jiě shén me gōng néng cān shù zhǐ shì tōng guò kàn yuán xíng fǒu zhǎo dào shí de hán shù dìng

Tip: You can easily create function prototypes by using copy/paste on your function declaration. Don’t forget the semicolon on the end.

提示:您可以使用复制/粘贴功能声明来轻松创建功能原型。别忘了在后面的分号。

shì nín shǐ使 yòng zhì / zhān tiē gōng néng shēng míng lái qīng sōng chuàng jiàn gōng néng yuán xíng bié wàng le zài hòu mian de fēn hào

Forgetting the function body

忘记了身体

wàng le shēn

One question many new programmers have is: what happens if we forward declare a function but do not define it?

许多新程序员都有一个问题是:如果我们向前声明一个函数,但不定义它,会发生什么?

duō xīn chéng yuán dōu yǒu wèn shì guǒ men xiàng qián shēng míng hán shù dàn dìng huì shēng shén me

The answer is: it depends. If a forward declaration is made, but the function is never called, the program will compile and run fine. However, if a forward declaration is made, the function is called, but the program never defines the function, the program will compile okay, but the linker will complain that it can’t resolve the function call.

答案是:这取决于。如果有一个向前声明,但该函数是从未调用的,程序将编译和运行良好。但是,如果一个向前声明,该函数被调用,但程序没有定义函数,程序将编译好,但连接器会抱怨它不能解决函数调用。

àn shì zhè jué guǒ yǒu xiàng qián shēng míng dàn gāi hán shù shì cóng wèi diào yòng de chéng jiāng biān yùn xíng liáng hǎo dàn shì guǒ xiàng qián shēng míng gāi hán shù bèi diào yòng dàn chéng méi yǒu dìng hán shù chéng jiāng biān hǎo dàn lián jiē huì bào yuàn néng jiě jué hán shù diào yòng

Consider the following program:

考虑以下程序:

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

你可以看到,程序编译好的,但是它没有在链接阶段(int,int,int加)没有定义。

kàn dào chéng biān hǎo de dàn shì méi yǒu zài liàn jiē jiē duàn i n t i n t i n t jiā méi yǒu dìng

Other types of forward declarations

其他类型的前瞻性声明

lèi xíng de qián zhān xìng shēng míng

Forward declarations are most often used with functions. However, forward declarations can also be used with other identifiers in C++, such as variables and user-defined types. Other types of identifiers (e.g. user-defined types) have a different syntax for forward declaration.

正向声明是最常用的函数。然而,前向声明也适用于C++的其他标识符,如变量和用户自定义类型。其他类型的标识符(如用户定义的类型)有一个不同的语法声明。

zhèng xiàng shēng míng shì zuì cháng yòng de hán shù rán ér qián xiàng shēng míng shì yòng C + + de biāo shí biàn liàng yòng dìng lèi xíng lèi xíng de biāo shí yòng dìng de lèi xíng yǒu tóng de shēng míng

We’ll talk more about how to forward declare other types of identifiers in future lessons.

在今后的课程中,我们将讨论如何向前声明其他类型的标识符。

zài jīn hòu de chéng zhōng men jiāng tǎo lùn xiàng qián shēng míng lèi xíng de biāo shí

Declarations vs. definitions

声明与定义

shēng míng dìng

In C++, you’ll often hear the words “declaration” and “definition” used. What do they mean? You now have enough of a framework to understand the difference between the two.

在C++中,你经常会听到“宣言”和“定义”。他们是什么意思?你现在有足够的框架来理解两者之间的差异。

zài C + + zhōng jīng cháng huì tīng dào xuān yán dìng men shì shén me si xiàn zài yǒu gòu de kuàng jià lái jiě liǎng zhě zhī jiān de chā

A definition actually implements or instantiates (causes memory to be allocated for) the identifier. Here are some examples of definitions:

定义实际实现或实例化(导致内存被分配的标识符)。下面是一些定义的例子:

int add(int x, int y) // defines function add()
{
    return x + y;
}
 
int x; // instantiates (causes memory to be allocated for) an integer variable named x


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值