I keep getting "p is undefined" warning. Why is that happening? p is an object of class Point which I defined under int main(){}.
int main() {
float x_, y_, z_;
Point p[3];
// the rest of the code
}
How can I use p outside of int main(){} ?
CodePudding user response:
you need to pass the variable 'p' of class Point to access it inside the Space function just like you are passing variables 'a' and 'b'. Like this Point Space(Point a, Point b, Point p) { // rest of the code }
CodePudding user response:
C has something known as 'scope'. A scope cannot access a variable defined outside of it. You'll need to pass p into the function itself as such:
Point Space (const Point& p, Point a, Point b)
I am using const Point& p
instead of Point p
because in the function you're not modifying the value of p
. So making p
a const
variable will be better (as it doesn't allow a value to be modified). And I am using &
here to make p
a reference variable, which can save memory.