C++ 问题声明外部类对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8910047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:18:05  来源:igfitidea点击:

Issue declaring extern class object

c++classundefinedextern

提问by Jo?o Silva

Let me start by saying I've extensively searched for answers on google and more specifically here.

首先让我说我已经在谷歌上广泛搜索了答案,更具体地说是在这里。

The thing is I actually (at least I think I did) found people with similar problems, though the answer given to them gave me another problem.

问题是我实际上(至少我认为我做到了)发现了有类似问题的人,尽管给他们的答案给了我另一个问题。

I'm using Visual Studio 2010 Express and working with SFML libary (though i do not think this last part is relevant)

我正在使用 Visual Studio 2010 Express 并使用 SFML 库(尽管我认为这最后一部分无关紧要)

So here it goes:

所以这里是这样的:

I have a source file called player.cpp which holds class Player and I have a header file (included in all source files) called cc.h(command and control) that holds all the necessary includes and external variables/functions. The essential code can be summed up in the following:

我有一个名为 player.cpp 的源文件,它包含 Player 类,我有一个名为 cc.h(命令和控制)的头文件(包含在所有源文件中),其中包含所有必要的包含和外部变量/函数。基本代码可以总结如下:

Player.cpp:

播放器.cpp:

#include "cc.h"
class Player
{
private:

//some variables here

public:

//more variables and some functions

}john;//example instance

cc.h:

抄送:

#pragma once

//some #includes
//some externs

extern Player john;

Now in cc.h the word Player is underlined as a mistake saying it is an undefined identifier, but only sometimes, other times visual studio doesn't mark it as a mistake, instead it recognizes it as a class but doesn't recognize john as an object/instance (i hope it's called this way) of that same class. Furthermore, at compiling the first error it shows is "error C2146: syntax error : missing ';' before identifier 'john'" at the line of the extern declaration of john, in cc.h, which apparently (to me) does not make any sense.

现在在 cc.h 中, Player 这个词被加下划线作为一个错误,表示它是一个未定义的标识符,但只是有时,有时visual studio 不会将其标记为错误,而是将其识别为一个类但不识别 john作为同一个类的对象/实例(我希望它被称为这种方式)。此外,在编译第一个错误时,它显示error C2146: syntax error : missing ';' before identifier 'john'在 cc.h 中 john 的 extern 声明行中的“ ”,这显然(对我而言)没有任何意义。

回答by Coder02

The global declaration in cc.h would not help you, I guess - because you declare it to access it from else where (other than Player.cpp), but for this you need the method signatures - a soon as you want to access johnfrom elsewhere and thus include Player.cpp, you get duplicates symbols.

我猜 cc.h 中的全局声明对您没有帮助-因为您声明它是从其他地方(Player.cpp 除外)访问它,但是为此您需要方法签名-只要您想john从在其他地方,因此包括 Player.cpp,你会得到重复的符号。

Please consider creating a Player.hfile where only the class and method signatures are declared - like this:

请考虑创建一个Player.h仅声明类和方法签名的文件 - 如下所示:

#ifndef PLAYER_H_
#define PLAYER_H_

class Player
{
     void doSomething();
};
#endif

and add this to cc.h:

并将其添加到 cc.h:

#include <Player.h>
extern Player john;

and in your Player.cpp

并在您的 Player.cpp 中

#include <Player.h>

Player john;

void Player::doSomething()
{
    //...
}

This makes sure that the Playersignatures are known and a valid instance is declared globally.

这确保了Player签名是已知的,并且全局声明了一个有效的实例。

回答by Oliver Charlesworth

You need to put the definition of your Playerclass in the header file, before you declare the externvariable. Otherwise the compiler has no idea what Playeris.

Player在声明extern变量之前,您需要将类的定义放在头文件中。否则编译器不知道是什么Player

I suggest something like this:

我建议这样的事情:

player.h

播放器.h

#ifndef PLAYER_H_
#define PLAYER_H_

class Player {
    ...
};

#endif

player.cpp

播放器.cpp

#include "player.h"

Player john;

cc.h

cc.h

#ifndef CC_H_
#define CC_H_

#include "player.h"

extern Player john;

#endif

回答by user989583

  1. You need to define the Playerclass, in your header file
  2. Use externto use variable that has an external linkage, and is already defined in some other file.
  1. 您需要Player在头文件中定义类
  2. 使用extern到具有外部链接,并已在其他一些文件中定义使用的变量。

For example: you have file a.cpp, and inside this file has a global variable Player p. If you want to use the same exact instance pof Playerin file c.cpp, then inside file c.cppyou write extern Player p.

例如:你有 file a.cpp,并且在这个文件中有一个全局变量Player p。如果你想使用完全相同的情况下pPlayer文件c.cpp,那么文件里面c.cpp你写的extern Player p

I hope i made myself clear.

我希望我说清楚了。

回答by Rachitha

"extern Player john;"is considered to be undefined identifier as the compiler is unable to understand what Playeris, as you have not included the file Player.cppwhere the class Player is declared to cc.h. It is always recommended to declare the class and its methods in header files say for example in Player.h and then define these methods in the source file i.e Player.cpp. And to include Player.h in your cc.h so that compiler understands where " Player john;"is declared.

“外援约翰;” 被认为是未定义的标识符,因为编译器无法理解Player是什么,因为您没有包含Player.cpp文件,其中Player.cpp类被声明为cc.h。始终建议在头文件中声明类及其方法,例如在 Player.h 中,然后在源文件中定义这些方法,即 Player.cpp。并将 Player.h 包含在您的 cc.h 中,以便编译器了解“ Player john;”的位置。被宣布。