跨多个 .cpp 文件共享全局变量(visual studio 2015)
sharing global variables across multiple .cpp files (visual studio 2015)
我正在 visual studio 中编写一个程序 (c++),其中包含多个函数,我想将其分布在多个 .cpp 文件中。我最初将所有功能(整个程序)包含在一个 .cpp 文件中并且工作正常,但现在当我尝试分解它时我 运行 陷入各种错误。
我有一些全局变量,它们最初是在单个 .cpp 文件的顶部定义的,但现在当我将一些使用这些常量变量的函数移动到单独的 .cpp 文件中时,我遇到了错误。
如何在多个 .cpp 文件中共享这些常量变量?我是否应该创建一个包含所有常量全局变量的头文件,然后在使用它们的每个 .cpp 文件中以某种方式引用它们?
此外,由于我编写的一些函数调用了我也编写的其他函数,我怎样才能将这些函数拆分到单独的 .cpp 文件中并使一切仍然有效?我是否应该在我想调用它们的每个 .cpp 文件中制作所有必要函数的原型?
这里是我想要制作的一般格式:
main.cpp 文件:
int main()
{
//calling functions such as minimax(), printBoard(), and others
//using global variables such as const in w_ or const int pDisc
}
minimax.cpp 文件:
void minimax()
{
//code for minimax function
//minimax () calls other functions such as winDetect() and playMove()
//using global variables such as const in w_ or const int pDisc
}
helper_func.cpp 文件:
winDetect(){//definition}
playMove(){//definition}
printBoard(){//definition}
//using global variables such as const in w_ or const int pDisc
我还有以下头文件,我想将它们包含在每个 .cpp 文件中(不确定我是否需要将它们复制并粘贴到每个 .cpp 文件中,或者是否有办法将它们全局包含) :
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <cstdlib>
在此先感谢您的帮助!如果需要有关我如何组织该程序的更多信息,请告诉我!
编辑:
我尝试设置一个头文件(称为 con4.h),但我在 con4.h 中的每个函数声明中都收到一个错误 "expression must have a constant value"。对于某些声明,错误会抛出不止一次。这是完整的代码:
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <cstdlib>
#ifndef CON4_H
#define CON4_H
//pDisc = disc; cDisc = computer disc; nDisc = no disc
const int pDisc = 1, cDisc = 2, nDisc = 0;
//width and height variables
const int w_ = 7, h_ = 6;
//Base number of maximum iterations (depending on the stage in the game, recursion may go more or less deep)
const int MAX_ITER = 7;
//Values for SetConsoleTextAttribute()
HANDLE H = GetStdHandle(STD_OUTPUT_HANDLE);
const int BLACK = 0;
const int BLUE = 1;
const int GREEN = 2;
const int CYAN = 3;
const int RED = 4;
const int MAGENTA = 5;
const int BROWN = 6;
const int LIGHTGRAY = 7;
const int DARKGRAY = 8;
const int LIGHTBLUE = 9;
const int LIGHTGREEN = 10;
const int LIGHTCYAN = 11;
const int LIGHTRED = 12;
const int LIGHTMAGENTA = 13;
const int YELLOW = 14;
const int WHITE = 15;
//Declarations (but not definitions) for all functions
#endif
您应该在 header 中包含一个扩展名为 .h 的文件,这意味着将从您的 cpp 文件复制您的代码并放入主文件中。
Should I create a header file that contains all the constant global
variables and then reference them somehow in each .cpp file in which
they are used?
yes.please that.consider 也使用名称空间对它们进行分组。
Also, as some of the functions I have written call upon other
functions which I have also written, how can I split these functions
into separate .cpp files and have everything still work?
一般来说,通常是通过将问题划分为易于管理的部分来开始设计程序。这些部分中的每一个都可以作为一个或多个功能来实现。每个部分的所有功能通常都在一个文件中。
比如说,在你的情况下:
//board.h
void printBoard(int board[][w_]);
void copyBoard(int board[][w_], int newBoard[][w_]);
//color.h
void printColor(string str, int color);
void printColor(int i, int color);
void printColor(char c, int color);
//algo.h
bool playMove(int board[][w_], int col, int who);
bool winDetect(int board[][w_], int who);
void minimax(int board[][w_], int score[], int who, int currentCheck, int iter);
//main.cpp
//include all header files and combine the logic
在面向对象的术语中,您可以通过创建棋盘、颜色和算法 类 然后组合逻辑来实现上述逻辑。
我正在 visual studio 中编写一个程序 (c++),其中包含多个函数,我想将其分布在多个 .cpp 文件中。我最初将所有功能(整个程序)包含在一个 .cpp 文件中并且工作正常,但现在当我尝试分解它时我 运行 陷入各种错误。
我有一些全局变量,它们最初是在单个 .cpp 文件的顶部定义的,但现在当我将一些使用这些常量变量的函数移动到单独的 .cpp 文件中时,我遇到了错误。
如何在多个 .cpp 文件中共享这些常量变量?我是否应该创建一个包含所有常量全局变量的头文件,然后在使用它们的每个 .cpp 文件中以某种方式引用它们?
此外,由于我编写的一些函数调用了我也编写的其他函数,我怎样才能将这些函数拆分到单独的 .cpp 文件中并使一切仍然有效?我是否应该在我想调用它们的每个 .cpp 文件中制作所有必要函数的原型?
这里是我想要制作的一般格式:
main.cpp 文件:
int main()
{
//calling functions such as minimax(), printBoard(), and others
//using global variables such as const in w_ or const int pDisc
}
minimax.cpp 文件:
void minimax()
{
//code for minimax function
//minimax () calls other functions such as winDetect() and playMove()
//using global variables such as const in w_ or const int pDisc
}
helper_func.cpp 文件:
winDetect(){//definition}
playMove(){//definition}
printBoard(){//definition}
//using global variables such as const in w_ or const int pDisc
我还有以下头文件,我想将它们包含在每个 .cpp 文件中(不确定我是否需要将它们复制并粘贴到每个 .cpp 文件中,或者是否有办法将它们全局包含) :
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <cstdlib>
在此先感谢您的帮助!如果需要有关我如何组织该程序的更多信息,请告诉我!
编辑:
我尝试设置一个头文件(称为 con4.h),但我在 con4.h 中的每个函数声明中都收到一个错误 "expression must have a constant value"。对于某些声明,错误会抛出不止一次。这是完整的代码:
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <cstdlib>
#ifndef CON4_H
#define CON4_H
//pDisc = disc; cDisc = computer disc; nDisc = no disc
const int pDisc = 1, cDisc = 2, nDisc = 0;
//width and height variables
const int w_ = 7, h_ = 6;
//Base number of maximum iterations (depending on the stage in the game, recursion may go more or less deep)
const int MAX_ITER = 7;
//Values for SetConsoleTextAttribute()
HANDLE H = GetStdHandle(STD_OUTPUT_HANDLE);
const int BLACK = 0;
const int BLUE = 1;
const int GREEN = 2;
const int CYAN = 3;
const int RED = 4;
const int MAGENTA = 5;
const int BROWN = 6;
const int LIGHTGRAY = 7;
const int DARKGRAY = 8;
const int LIGHTBLUE = 9;
const int LIGHTGREEN = 10;
const int LIGHTCYAN = 11;
const int LIGHTRED = 12;
const int LIGHTMAGENTA = 13;
const int YELLOW = 14;
const int WHITE = 15;
//Declarations (but not definitions) for all functions
#endif
您应该在 header 中包含一个扩展名为 .h 的文件,这意味着将从您的 cpp 文件复制您的代码并放入主文件中。
Should I create a header file that contains all the constant global variables and then reference them somehow in each .cpp file in which they are used?
yes.please that.consider 也使用名称空间对它们进行分组。
Also, as some of the functions I have written call upon other functions which I have also written, how can I split these functions into separate .cpp files and have everything still work?
一般来说,通常是通过将问题划分为易于管理的部分来开始设计程序。这些部分中的每一个都可以作为一个或多个功能来实现。每个部分的所有功能通常都在一个文件中。
比如说,在你的情况下:
//board.h
void printBoard(int board[][w_]);
void copyBoard(int board[][w_], int newBoard[][w_]);
//color.h
void printColor(string str, int color);
void printColor(int i, int color);
void printColor(char c, int color);
//algo.h
bool playMove(int board[][w_], int col, int who);
bool winDetect(int board[][w_], int who);
void minimax(int board[][w_], int score[], int who, int currentCheck, int iter);
//main.cpp
//include all header files and combine the logic
在面向对象的术语中,您可以通过创建棋盘、颜色和算法 类 然后组合逻辑来实现上述逻辑。