c++ making coord globally accesible -
just real quicky, feel might basic question, can't wrap head around how make work. i've declared 2 coordinate points in:
int x = (0); int y = (0); coord coord; coord.x = x; coord.y = y;
they have been declared prior main, need globally accessible further functions within program, getting error messages when trying set coord.x/y, saying declaration has no storage type. can fix this?
int x = (0); int y = (0); coord coord;
these definitions of global variables, initialisation literal values first two.
coord.x = x; coord.y = y;
these statements.
you cannot have statements outside of function, need put function e.g. main
.
but initialise member fields of instance of class coord
use constructor of class:
struct coord { int x; int y; coord(int x, int y) : x(x), y(y) { } }; coord p = coord (21, 42);
but in case wouldn't need constructor @ all, can use structure initialisation:
struct coord { int x; int y; }; coord q = {42, 21}; coord p{21, 42}; // universal construction
but, finally, highly doubt need global variable. should check overall design. (though of above unrelated whether use initialise global or local or member variable)
Comments
Post a Comment