Support Forums
Problem with a member function of a class - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: Problem with a member function of a class (/showthread.php?tid=2330)



Problem with a member function of a class - charnet3d - 10-29-2009

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 ^^


RE: Problem with a member function of a class - Gaijin - 10-29-2009

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++.


RE: Problem with a member function of a class - charnet3d - 10-31-2009

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 ^^