1
votes

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
2
@EdHeal: Shouldn't*?Lightness Races in Orbit
Opps . Sorry. Hate this predicted textEd Heal
You shouldn't have usedone global variables in the first place. Take the opportunity to remove themEd Heal

2 Answers

0
votes

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 do that.consider using namespaces too to group them.

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?

Generally, the norm is to start designing a program by dividing the problem into easily managed sections. Each of these sections might be implemented as one or more functions. All functions from each section usually live in a single file.

Say, in your case:

//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

In object oriented terms, you can implement the above logic by creating board, color and algo classes and then combining the logic.

0
votes

You shoul include in header a file with .h extension, this mean will copy your code from your cpp file and put in main file.

Please take a look here c++ including .cpp files