简体   繁体   English

C ++中的外部链接

[英]External Linkage in c++

I'm making a simple program dealing with poker hands and probabilities. 我正在编写一个处理扑克手和概率的简单程序。 I'm running test cases on various hands, and in my program I need a deck from which to draw my hand that is constant and global. 我正在各种手上运行测试用例,在我的程序中,我需要一个从中汲取恒久而全局的手的平台。 My program has three main objects: Card, Hand, and Deck. 我的程序有三个主要对象:Card,Hand和Deck。 I am trying to create a global deck but it seems no matter what I do I get an error saying globalDeck is undefined in hand.cpp. 我正在尝试创建一个全局卡片组,但是无论我做什么,似乎都收到一条错误消息,说在hand.cpp中未定义globalDeck。 Here is the relevant code: 以下是相关代码:

hand.cpp: hand.cpp:

Hand::Hand(int &one, int &two, int &three, int &four, int &five)
{
    cardsinHand.push_back(globalDeck.copy(one));
    cardsinHand.push_back(globalDeck.copy(two));
    cardsinHand.push_back(globalDeck.copy(three));
    cardsinHand.push_back(globalDeck.copy(four));
    cardsinHand.push_back(globalDeck.copy(five));
}

and my cardgames.cpp file: 和我的cardgames.cpp文件:

#include "stdafx.h"
#include<iostream>
#include "hand.h"
using namespace std;

Deck globalDeck;

void printresults(long successes, long trials)
......

Note: I have tried replacing Deck globalDeck with extern Deck globalDeck, extern const globalDeck, etc etc. Nothing seems to make it so that globalDeck is defined in hand.cpp. 注意:我已经尝试用extern Deck globalDeck,extern const globalDeck等替换Deck globalDeck。似乎什么也没做,所以在hand.cpp中定义了globalDeck。 Also, the deck will stay constant throughout the program, so it could be declared as a global constant, or a global variable, whichever is easier. 另外,在整个程序中,deck将保持不变,因此可以将其声明为全局常数或全局变量,以较容易的一个为准。 My error output is: 我的错误输出是:

1>c:\users\\desktop\semesters past\winter 2013\computer programming\projects\cardgames\cardgames\hand.cpp(31): error C2065: 'globalDeck' : undeclared identifier
1>c:\users\\desktop\semesters past\winter 2013\computer programming\projects\cardgames\cardgames\hand.cpp(31): error C2228: left of '.copy' must have class/struct/union
1>          type is 'unknown-type'

Declare your global variable in one header file 在一个头文件中声明全局变量

extern Deck globalDeck;

and define it in cardgames.cpp 在cardgames.cpp中定义

Deck globalDeck;

Explanation:The compiler has to see what globalDeck is before you can use it. 说明:编译器必须先查看globalDeck是什么,然后才能使用它。 And the compiler mostly processes each *.cpp individually, without looking at others. 而且,编译器通常会单独处理每个* .cpp,而无需查看其他文件。 In header files you can say what things are in the program without giving the complete and unique definition. 在头文件中,您可以说出程序中的内容而无需给出完整且唯一的定义。

The global variable globalDeck needs to be declared in every compilation unit (.cpp file) that uses it. 全局变量globalDeck需要在使用它的每一个编译单元(.cpp文件)中声明 It needs to be defined in exactly one compilation unit. 它需要在一个编译单元中定义

A declaration of the form 表格声明

Deck globalDeck;

is both a declaration and a definition. 既是声明是定义。 A declaration with extern like: 带有extern的声明,例如:

extern Deck globalDeck;

is just a declaration and not a definition. 只是声明而不是定义。

Header files (.h files) are just text that is included in other files with #include . 头文件(.h文件)仅是#include包含在其他文件中的文本。 So you can put the declaration extern Decl globalDeck; 因此,您可以将声明extern Decl globalDeck; in a header file (any header file) and #include that header file in every .cpp file. 在头文件(任何头文件)中,并在每个.cpp文件中#include该头文件。 You will also need the definition in exactly one .cpp file. 您还将需要仅在一个.cpp文件中的定义。

class Deck has not been defined in hand.cpp because a header is missing. 由于缺少标头,因此尚未在hand.cpp定义class Deck You need to add something like #include "hand.h" or #include "deck.h" . 您需要添加#include "hand.h"#include "deck.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM