0

So, the problem happens around line 35 (where I call the edgeList constructor). Whenever it gets to that point I get the error

main.cpp:35:60: error: no matching function for call to 'point::point()'

Here's the problem area

class edgeList {
   private:
      point origin;
      point terminal;
      double weight;
   public:
      edgeList(const point& origin, const point& terminal) {
         this->origin = origin;
         this->terminal = terminal;
         this->weight = findWeight(origin, terminal);
      }
      int findWeight(point origin, point terminal) {
         return sqrt(pow((terminal.place('x') - origin.place('x')), 2) + pow((terminal.place('y') - origin.place('y')), 2));
      }
};

And here's the full code

#include <iostream>
#include <string.h>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;

class point {                                                       //Creates the class that stores x and y coordinates
   private:
      double x;
      double y;
   public:
      point(double x, double y) {
         this->x = x;
         this->y = y;
      }
      double place(char c) {
          if (c == 'x') {
             return this->x;
          }
          else if (c == 'y') {
             return this->y;
          }
          else {
             return -1;
          }
      }
};
class edgeList {
   private:
      point origin;
      point terminal;
      double weight;
   public:
      edgeList(const point& origin, const point& terminal) {
         this->origin = origin;
         this->terminal = terminal;
         this->weight = findWeight(origin, terminal);
      }
      int findWeight(point origin, point terminal) {
         return sqrt(pow((terminal.place('x') - origin.place('x')), 2) + pow((terminal.place('y') - origin.place('y')), 2));
      }
};

int main(int argc, char *argv[]) {

   double x;
   double y;
   int i = 0;                                                       //FIXME: DELETE THIS AFTER TESTING
   vector<point> origin;
   if (argv[1] == NULL) {
      cout << "ERROR: NO FILE NAME FOUND" << endl;
      return -1;
   }
   if (strcmp(argv[1], "random") != 0) {
      cout << argv[1] << endl;
      ifstream fp;
      fp.open(argv[1]);
      if (!fp.is_open()) {
         cout << "ERROR: NO FILE FOUND" << endl;
         return -1;
      }
      while (!fp.eof()) {
         fp >> x;
         cout << x << " ";
         fp >> y;
         cout << y << endl;
         point pnt(x,y);
         origin.push_back(pnt);
         cout << "Point children" << endl << origin[i].place('x') << " " << origin[i].place('y') << endl;
         i++;                                                       //FIXME: DELETE THIS AFTER TESTING
      }
      edgeList z(origin[0], origin[1]);
   }
   return 0;
}

What am I doing wrong here?

David
  • 88
  • 5

1 Answers1

0

You're not initialising any of your class edgeList's members.

Well, you are, but by default. Doing that requires a default constructor, which point doesn't have. Hence the error.

Those three lines in the edgeList constructor are assignments after-the-fact:

  edgeList(const point& origin, const point& terminal) {
     this->origin = origin;
     this->terminal = terminal;
     this->weight = findWeight(origin, terminal);
  }

Here's what you're supposed to do, to initialise members:

  edgeList(const point& origin, const point& terminal)
     : origin(origin)
     , terminal(terminal)
     , weight(findWeight(origin, terminal))
  {}

Currently your code is more like:

  edgeList(const point& origin, const point& terminal)
     : origin()
     , terminal()
     , weight()
  {
     this->origin = origin;
     this->terminal = terminal;
     this->weight = findWeight(origin, terminal);
  }

If your C++ book doesn't explain this, you need a better one!

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055