不能解决函数调用!

来源:互联网 发布:mac上电话轰炸 编辑:程序博客网 时间:2024/06/10 01:18

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

ránérmengènghuanmendecānshùyīnwèiyǔnnínliǎojiěshénmegōngnéngcānshùzhǐshìtōngguòkànyuánxíngfǒuzhǎodàoshídehánshù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ínshǐ使yòngzhì/zhāntiēgōngnéngshēngmíngláiqīngsōngchuàngjiàngōngnéngyuánxíngbiéwànglezàihòumiandefēnhào

Forgetting the function body

忘记了身体

wàngleshēn

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

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

duōxīnchéngyuándōuyǒuwènshìguǒmenxiàngqiánshēngmínghánshùdàndìnghuìshēngshénme

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.

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

ànshìzhèjuéguǒyǒuxiàngqiánshēngmíngdàngāihánshùshìcóngwèidiàoyòngdechéngjiāngbiānyùnxíngliánghǎodànshìguǒxiàngqiánshēngmínggāihánshùbèidiàoyòngdànchéngméiyǒudìnghánshùchéngjiāngbiānhǎodànliánjiēhuìbàoyuànnéngjiějuéhánshùdiàoyò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àndàochéngbiānhǎodedànshìméiyǒuzàiliànjiējiēduànintintintjiāméiyǒudìng

Other types of forward declarations

其他类型的前瞻性声明

lèixíngdeqiánzhānxìngshēngmí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èngxiàngshēngmíngshìzuìchángyòngdehánshùránérqiánxiàngshēngmíngshìyòngC++debiāoshíbiànliàngyòngdìnglèixínglèixíngdebiāoshíyòngdìngdelèixíngyǒutóngdeshēngmíng

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

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

zàijīnhòudechéngzhōngmenjiāngtǎolùnxiàngqiánshēngmínglèixíngdebiāoshí

Declarations vs. definitions

声明与定义

shēngmíngdì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àiC++zhōngjīngchánghuìtīngdàoxuānyándìngmenshìshénmesixiànzàiyǒugòudekuàngjiàláijiěliǎngzhězhījiāndechā

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


0 0