Class/member function error

c++, class, member-functions

Solution

This function:

int dir_mag(double dir, double mag) :direction(dir), magnitude(dir)
{return 0; };

is using an initializer list (`:direction(dir), magnitude(dir)`) and that's only allowed for constructors. If you had planned to make this a constructor your class should look like this:

class physics_vector
{ 
public:
    double direction, magnitude;
    physics_vector(double dir, double mag) :direction(dir), 
        magnitude(dir) {};
};

And that will compile. Note that you are not allowed a return value from a constructor, nor do they have return types.

Problem

I have this snippet of code here: ``` class physics_vector { public: double direction, magnitude; int dir_mag(double dir, double mag) :direction(dir), magnitude(dir) {return 0; }; }; int dir_mag(double dir, double mag) { cout << "Direction: " << dir << '\n'; cout << "Magnitude: " << mag << '\n'; return 0; } ``` Whenever I try to compile I get the error, ``` 13:39: error: only constructors take member initializers ``` Any help please?

Original source