c++ - 跨多个 .cpp 文件共享全局变量(visual studio 2015)

标签 c++ header include visual-studio-2015

我正在 visual studio 中编写一个具有多个函数的程序 (c++),我想将其分布在多个 .cpp 文件中。我最初将所有功能(整个程序)包含在一个 .cpp 文件中并且它工作正常,但现在当我试图分解它时我遇到了各种各样的错误。

我有一些全局变量,它们最初是在单个 .cpp 文件的顶部定义的,但现在当我将一些使用这些常量变量的函数移动到单独的 .cpp 文件中时,我遇到了错误。

如何在多个 .cpp 文件中共享这些常量变量?我是否应该创建一个包含所有常量全局变量的头文件,然后在使用它们的每个 .cpp 文件中以某种方式引用它们?

此外,由于我编写的一些函数调用了我也编写的其他函数,我如何才能将这些函数拆分到单独的 .cpp 文件中并使一切仍然有效?我是否应该在我想调用它们的每个 .cpp 文件中对所有必要的函数进行原型(prototype)设计?

这里是我想要制作的一般格式:

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 中的每个函数声明中都收到错误,提示“表达式必须具有常量值”。对于某些声明,错误会抛出不止一次。这是完整的代码:

#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

最佳答案

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?

是的。请这样做。考虑也使用命名空间来对它们进行分组。

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

在面向对象的术语中,您可以通过创建棋盘、颜色和算法类然后组合逻辑来实现上述逻辑。

关于c++ - 跨多个 .cpp 文件共享全局变量(visual studio 2015),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043474/

相关文章:

c++ - 带有定义参数的嵌套定义C++

c++ - 在另一个类 C++ 中用作成员的类的 header 包含问题

c++ - 使用 STL 类而不包含其适当的 header

c++ - 仅用于标题项目的 CMakeList 并在另一个项目中使用它

c++ - 如何连接标题中的字符串?

c++ - 强制 C++ 使用本地头文件中的函数

c++ - 为了成为一名 C++ 程序员,我应该始终使用 std::vector 而不是创建自己的链表吗?

c++ - 尝试理解 C++14 中的 §7.2/6

C++ 文件系统迭代器无效参数

c++ - 我应该把第三方库放在哪里?