I have the following code and the objective is to make use of the colour variable as a binary flag in the code.
main.cpp
#include "turn.h"
#include <stdio.h>
void subfunc(Turns t) {
printf("%d\n", t);
}
void func() {
Turns colour = getTurn(&turn);
subfunc(colour);
}
int main() {
setTurn(&turn, WHITE);
func();
return 0;
}
turn.cpp
#include "turn.h"
extern Turn turn;
void setTurn(Turn *t, Turns newTurn) {
t->turn = newTurn;
}
Turns getTurn(Turn *t) {
return t->turn;
}
void changeTurn(Turn *t) {
if (t->turn == WHITE) {
t->turn = BLACK;
} else {
t->turn = WHITE;
}
}
turn.h
#ifndef TURN_H
#define TURN_H
enum Turns {WHITE, BLACK};
typedef struct {
Turns turn;
} Turn;
void setTurn(Turn *t, Turns newTurn);
Turns getTurn(Turn *t);
void changeTurn(Turn *t);
#endif
Unfortunately this code gives me an error stating that turn was not declared in the main.cpp scope.
func(),turnis supposed to be an object of typeTurn. But there is no such object in scope there.You have the same issue inmain(). You can create such an object inmain:Turn turn;and then pass it tofunc()if you add a parameter.extern Turn turn;