Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with a member function of a class
#1
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 ^^
Reply
#2
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++.
Reply
#3
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 ^^
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  C++ stdio.h rename function problem AceInfinity 2 1,146 01-14-2012, 08:39 PM
Last Post: AceInfinity
  [C] Factorial function Commodore 1 1,029 11-28-2010, 07:13 AM
Last Post: RANA
  [C] Prime number function Commodore 2 5,220 10-11-2010, 01:11 PM
Last Post: Commodore

Forum Jump:


Users browsing this thread: 2 Guest(s)