c - Passing enum variable to a function -
hi have function below.
void turnright(enum direction heading, int x, int y){ if(y<=49 && heading==toright){ heading=todown; x=x+1; } else if(x<=49 && heading==todown){ heading=toleft; y=y-1; } else if(x>0 && heading==toleft){ heading=toup; x=x-1; } else if(y<=49&&heading==toup){ heading=toright; y=y+1; } else printf("can not turn right! boundary alert!"); }//end function
i declared enum in main as:
enum direction {todown, toup, toright, toleft}; static enum direction heading; heading=toright;
and calling function main
turnright(heading, x, y);
and above added prototype as:
void turnright(enum direction heading, int x, int y);
but compiler spits below errors over:
error c2065: 'todown' : undeclared identifier error c2065: 'toup' : undeclared identifier error c2065: 'toleft' : undeclared identifier error c2065: 'toright' : undeclared identifier
can tell me doing wrong?
i declared enum in main
you need declare enum direction
in file included in translation units use enum
, not in main
. example, can define in direction.h
(don't forget add inclusion guards), , include in both main
, turn-processing source files:
direction.h
#ifndef direction_h #define direction_h enum direction {todown, toup, toright, toleft}; #endif
now add #include "direction.h"
main
, other files using enum
make sources compile correctly.
Comments
Post a Comment