当我尝试使用 extern 在多个源文件中设置变量时出现错误
I'm getting an error when I try to use extern to have variables across multiple source files
编辑:我忘了使用 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 个!)任何帮助将不胜感激!
您不能定义声明为extern
的变量。
extern 只是声明变量,说它存在,而不是定义它。变量应该在不同的地方定义。
所以extern int x=1;
是错误的。
Header:
extern int x;
其他地方的某处(可能是其他文件):
int x = 1;
或
int x;
x = SomeFunction();
编辑:我忘了使用 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 个!)任何帮助将不胜感激!
您不能定义声明为extern
的变量。
extern 只是声明变量,说它存在,而不是定义它。变量应该在不同的地方定义。
所以extern int x=1;
是错误的。
Header:
extern int x;
其他地方的某处(可能是其他文件):
int x = 1;
或
int x;
x = SomeFunction();