当我尝试使用extern在多个源文件中包含变量时,我收到错误

时间:2015-08-28 13:39:17

标签: c++ class header getline extern

编辑:我忘了使用std命名空间。这解决了一些错误,但不是全部。

我正在制作基于文字的游戏。最初,游戏是一个主要的源文件。现在,为了便于导航(游戏是开源的,我试图鼓励改装),我想把它分成几个文件。当然,要做到这一点,我需要适用于所有文件的变量。

所以,我把所有的全局变量,移动到一个头文件,并在它们上打了一个extern。是的,我在没有extern的主源文件中定义了它们。

以下是带有定义的头文件:

#include <string>

extern string start; //Starts the game.
extern int scenario; //Holds the random number that decides the scenario that plays.
extern int roomScenario; // Holds the random number that decides what room scenario plays.
extern int isRandom = 1; //Holds whether the map is randomized. On by default.
extern int isSeedOn;//Holds whether the user can input a seed.
extern int isCodes = 1; //Holds whether the codes system is on. On by default. 
extern int mapSeed; //The seed that the map creator uses.
extern int cD; //The current intersection the player is at. 0-15.
extern int deaths = 0; //The number of time the player has died. Zero by default.
extern int length = 16; //Length of catacombs. 16 by default.
extern int eLength = 15; //Effective length. Used to calculate whether the user is at the ladder.
extern int totalLives; //Total number of lives.
extern int lives; //Number of lives. Length / 2.
extern int livesRemain; //Number of lives remaining. It's value is equal to 'lives' - 'deaths'.
extern int lastDiedOn; //Hold the intersection the player last died at. Will be used to leave the sketchpad to the next player.
extern bool msgRead; //Hold whether the player has read the killer's message. Determines which ending you get.
extern bool takenSheet; //Stuff with fore/farsight
extern string ladderDecision; //The decision of the player when they're at the ladder.
extern string option; //Holds what the player chooses in the options menu.

以下是我遇到的一些错误:

在所有字符串变量上,我遇到了这两个错误:

Error C2146: syntax error : missing ';' before identifier '[VARIABLE_NAME]'

Error C4430: missing type specifier - int assumed.

extern不能用于字符串变量吗?如果没有,会有什么替代方案?

在我定义的所有字符串变量上(未在上面的头文件段中显示)我也得到了这个错误:

error C2440: 'initializing' : cannot convert from 'const char [VARIABLE_SIZE]' to 'int' 

同样,extern不能用于字符串变量吗?

在我的头文件中,我有三个类,还有一些数组。

class intersect{
public:
    string solve; //What the correct path is.
    bool isCoffin; //Holds if the room is Scenario_2.
    bool isSparkle; //Holds if the room is Scenario_1.
};
class foresight{
public:
    string msg; //The message of this code.
    string msg2; //The second message.
    int intCalled; //What intersection the code was found at. (Both of the codes would be found at the same intersection)
    bool isCalled; //If the the code has been found.
    bool isCalled2; //If the second code has been found.
};
class farsight{
public:
    string msg;
    string msg2;
    int intCalled;   //All of these have the same purpose as the ones of the same name in foresight. Look there for the documentation.
    bool isCalled;
    bool isCalled2;
};
extern intersect int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15, int16, int17, int18, int19, int20, int21, int22, int23, int24, int25, int26, int27, int28, int29, int30, int31, int32;
extern foresight fore1, fore2, fore3, fore4, fore5, fore6, fore7, fore8, fore9, fore10, fore11, fore12, fore13, fore14, fore15, fore16, fore17, fore18, fore19, fore20, fore21, fore22, fore23, fore24, fore25, fore26, fore27, fore28, fore29, fore30, fore31, fore32;
extern farsight far1, far2, far3, far4, far5, far6, far7, far8, far9, far10, far11, far12, far13, far14, far15, far16, far17, far18, far19, far20, far21, far22, far23, far24, far25, far26, far27, far28, far29, far30, far31, far32;
extern intersect ints[32] = { int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15, int16, int17, int18, int19, int20, int21, int22, int23, int24, int25, int26, int27, int28, int29, int30, int31, int32 };
extern string msgs[32] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12, msg13, msg14, msg15, msg16, msg17, msg18, msg19, msg20, msg21, msg22, msg23, msg24, msg25, msg26, msg27, msg28, msg29, msg30, msg31, msg32 };
extern foresight fore[32] = { fore1, fore2, fore3, fore4, fore5, fore6, fore7, fore8, fore9, fore10, fore11, fore12, fore13, fore14, fore15, fore16, fore17, fore18, fore19, fore20, fore21, fore22, fore23, fore24, fore25, fore26, fore27, fore28, fore29, fore30, fore31, fore32 };
extern farsight fars[32] = { far1, far2, far3, far4, far5, far6, far7, far8, far9, far10, far11, far12, far13, far14, far15, far16, far17, far18, far19, far20, far21, far22, far23, far24, far25, far26, far27, far28, far29, far30, far31, far32 };
extern string names[16] = { first1, first2, first3, first4, first5, first6, first7, first8, first9, first10, first11, first12, first13, first14, first15, first16 };

ints []中的每个变量(使用上面声明的32个交叉变量)数组,fore [](使用上面声明的32个前视变量)数组,以及fars [](使用声明的32个远视变量上面)数组生成错误。错误说数组中的变量重新声明上面声明的变量,只是它们在数组中。有趣的是,虽然...原始声明有相同的错误。好笑,对吧?

最后,头文件中的最后一个错误。当我将交叉变量中的一些(但不是全部,出于某些奇怪的原因)放入ints []数组时,会出现这些。

在int1和int4上,我收到错误:

IntelliSense: no suitable user-defined conversion from "intersect" to "std::string" exists

在int2和int3上,我收到错误:

IntelliSense: no suitable conversion function from "intersect" to "bool" exists

这与数据成员有关,对吧?

无论如何,在源文件中,我甚至会出现更多错误!嗯,只有一种类型。

每次我使用getline()时,都会收到此错误:

IntelliSense: no instance of overloaded function "getline" matches the argument list
        argument types are: (std::istream, <error-type>)

我会像这样使用getline():

getline(cin, start)

这里发生了什么?

值得庆幸的是,这就是所有这些,但它仍然是一个很大的问题。 (总共超过400个!)非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您无法定义声明为@Test public void test1() throws Throwable { success = false; final CountDownLatch signal = new CountDownLatch(1); task.onSuccess(new Continuation<Object, Object>() { @Override public Object then(Task<Object> task) throws Exception { if (condition == true) { success = true; } signal.countDown(); return null; } }); signal.await(30, TimeUnit.SECONDS); assertTrue(success); } 的变量。 Extern只声明变量,说它存在,而不是定义它。变量应该在不同的地方定义。 所以extern是错误的。

部首:

extern int x=1;

在其他地方某处(可能是其他文件):

extern int x;

 int x = 1;