Section16.10Defining Functions Outside of the Class
We previously learned that we can declare functions (provide the prototype) in one place and define them (write their full bodies) elsewhere. Doing so allows:
Us to define functions in any order we want (as long as we declare them first).
However, if we try to separate definitions of member functions from their declarations, we will run into an issue. Here is a failed attempt to move the Point constructorβs definition outside of the class:
Attempting to build the code fails. The compiler has no idea what line 11 is doing. It looks like a function called βPointβ that has no return type. And in that function, we use variables m_x and m_y without ever declaring them. There are similar issues with the getX function.
What the compiler needs is a hint that these functions are members of the class Point. We do that by adding the class name and the scope resolution operator :: to the function definitions. Saying Point::getX says βThis is the getX that belongs to Point.β And saying Point::Point says βThis is the constructors that belongs to Point.β Here is a working version of the code: