Support Forums

Full Version: Problem with a member function of a class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everybody,
I have been assigned a homework in C++, in which I have to create a class vector3d. Here's my code:

Code:
#include <iostream>
#include <math.h>

using namespace std;

class vector3d
{
    float x;
    float y;
    float z;
public:
    inline vector3d(float x = 0, float y = 0, float z = 0)
    {
        this->x = x;
        this->y = y;
        this->z = z;
    }

    vector3d& operator=(const vector3d& v)
    {
        x = v.x;
        y = v.y;
        z = v.z;
    }

    float norm() const
    {
        return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
    }

    void norm_compare(const vector3d & v, vector3d * r)
    {
        if (this->norm() > v.norm)
            *r = *this;
        else
            *r = v;
    }

    void print() const
    {
        cout << "x:" << x << endl;
        cout << "y:" << y << endl;
        cout << "z:" << z << endl;
    }
};

int main()
{
    vector3d v;
    vector3d v2(2, 5);
    vector3d v3;

    v2.norm_compare(v, &v3);
    v3.print();

    return 0;
}

I don't know what's wrong with it, it gives me the error "invalid use of member (did you forget the `&' ?)" in the first line "if (this->norm() > v.norm())" inside the member function norm_compare.
When I take away "v.norm" and replace it with a 0 it doesn't give the error, I don't understand, because that's supposed to work!

Thanks in advance for your help ^^
Well "norm" is a function and you forgot "()".

Code:
if (this->norm() > v.norm)
// TO
if (this->norm() > v.norm())

That complied on my pc with dev-c++.
ohhh what a mistake!!
Such subtleties make you go crazy, and the compiler notifies you about something far bigger hehe

Really thank very much, that saved me a lot of pain ^^