+ 1
How do I use variables from class in another class?
Example: class Class1{ public: int x = 7; }; class Class2{ public: int addition(int y = 5){ int sum = y + x; return sum; } }; //When I try to run something like this it tells me that x was not declared in this scope.
1 Answer
+ 1
In Class2, add:
Class1 class1instance;
and then call class property under the instance:
int sum = y + class1instance.x;