I'm writing a program (c++) in visual studio with multiple functions which I would like to spread across multiple .cpp files. I originally had all functions (the entire program) contained within one .cpp file and it was working fine, but now as I try to break it up I am running into all sorts of errors.
I have some global variables which I originally defined just at the top of the single .cpp file, but now as I move some functions, which use these constant variables, into separate .cpp files I get errors.
How can I share these constant variables across multiple .cpp files? 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? Should I prototype all necessary functions in each .cpp file in which I would like to call them?
Here is the general format which I would like to make work:
main.cpp file:
int main()
{
//calling functions such as minimax(), printBoard(), and others
//using global variables such as const in w_ or const int pDisc
}
minimax.cpp file:
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 file:
winDetect(){//definition}
playMove(){//definition}
printBoard(){//definition}
//using global variables such as const in w_ or const int pDisc
I also have the following header files which I would like to include in each .cpp file (not sure if I need to just copy and paste these into each .cpp file or if there's a way to include them globally):
#include <string>
#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <cstdlib>
Thanks in advance for any help! If any more info on how I've organized the program is needed, please let me know!
EDIT:
I tried setting up a header file (called con4.h), but I'm getting an error on every function declaration in con4.h that says "expression must have a constant value". For some declarations, the error gets thrown more than once. Here's the full code:
#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