Constructors and classes

This is a simple program describing two classes- a point and a vector using the point as datatype. I am also trying to use constructors in class point. But whenever I try to declare vector v1 it returns a compiler error. please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  #include <iostream>
  using namespace std;
  class point{
      public:
      double x,y;
      point(double nx, double ny){
          x = nx;
          y = ny;
          cout << "2-para construct";
      }
  };
  class vector{
      public:
      point start,end;
  };
  int main(){ 
      point p(3.0,6.0);
      vector s;  // gives an error 
  }



error for further reference-
classfour.cpp:18:12: error: use of deleted function 'vector::vector()'
18 | vector s;
| ^
classfour.cpp:12:7: note: 'vector::vector()' is implicitly deleted because the default definition would be ill-formed:
12 | class vector{
| ^~~~~~
classfour.cpp:12:7: error: no matching function for call to 'point::point()'
classfour.cpp:6:5: note: candidate: 'point::point(double, double)'
6 | point(double nx, double ny){
| ^~~~~
classfour.cpp:6:5: note: candidate expects 2 arguments, 0 provided
classfour.cpp:3:7: note: candidate: 'constexpr point::point(const point&)'
3 | class point{
| ^~~~~
classfour.cpp:3:7: note: candidate expects 1 argument, 0 provided
classfour.cpp:3:7: note: candidate: 'constexpr point::point(point&&)'
classfour.cpp:3:7: note: candidate expects 1 argument, 0 provided
classfour.cpp:12:7: error: no matching function for call to 'point::point()'
12 | class vector{
| ^~~~~~
classfour.cpp:6:5: note: candidate: 'point::point(double, double)'
6 | point(double nx, double ny){
| ^~~~~
classfour.cpp:6:5: note: candidate expects 2 arguments, 0 provided
classfour.cpp:3:7: note: candidate: 'constexpr point::point(const point&)'
3 | class point{
| ^~~~~
classfour.cpp:3:7: note: candidate expects 1 argument, 0 provided
classfour.cpp:3:7: note: candidate: 'constexpr point::point(point&&)'
classfour.cpp:3:7: note: candidate expects 1 argument, 0 provided
point has a two-arg constructor, so the 0-arg constructor is implicitly deleted.
Your vector class attempts to default-construct its point objects.

You could define a default constructor for your vector class
1
2
3
4
5
vector()
: start(0.0, 0.0), end(0.0, 0.0)
{

}
I am sorry but it's still giving an error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
  using namespace std;
  class point{
      public:
      double x,y;
      point(double nx, double ny){
          x = nx;
          y = ny;
          cout << "2-para construct";
      }
  };
  class vector{
      public:
      point start(0.0, 0.0), end(0.0, 0.0);
  };
  int main(){ 
      point p(3.0,6.0);
      vector s;  // gives an error 
  }
further, I am trying another method which is also giving error
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
class point{
    double x,y;
  public:
    point(double nx, double ny) : x(nx), y(ny) {}
};
class vector{
    point start, end;
  public:
    vector(double m, double n, double o, double p) : start.x(m), start.y(n), end.x(o), end.y(p) {}
};
int main(){ 
    vector s(1,2,3,4);
    cout << s.start.x;
    return 0;
}

please help
Last edited on
On the first post replace line 14 with point start{0.0, 0.0}, end{0.0, 0.0};
AFAIK, the problem is called vexing parsing error.
Topic archived. No new replies allowed.