0

I have classes called Point and Vector. I want to be able to initialize a Vector object from a Point object, which is created by calling its constructor. But I get the following error: the object gets initialized with non-class type ‘Vector(Point (*)())’

// in Point.h

class Point{
    double x,y,z;
public:
    Point();
    Point(double, double, double);
};

// in Vector.h

class Vector{
public:
    Vector();
    Vector(double, double, double);
    Vector(const Point&);
    f();
};

// in  main.cpp
int main(){
    Vector v(Point(0,0,0)); // OK
    Vector w(Point()); // 'error'
    v.f(); // OK
    w.f(); // error
}

Why does it not initialize the object 'w' using Point object created in default constructor of Point()? It is able to do similar thing for object 'v'.

I tried to use

 Vector w(new Point());

but this also gives compilation error, which I think I understand.

Jarod42
  • 203,559
  • 14
  • 181
  • 302

1 Answers1

3

Most vexing parse in action:

Vector w(Point()) is parsed as function declaration:

use {} instead:

Vector w{Point{}};
Jarod42
  • 203,559
  • 14
  • 181
  • 302