将对象传递给类并且不接收匹配的函数错误

时间:2017-05-03 19:02:21

标签: c++

所以,问题发生在第35行(我称之为edgeList构造函数)。每当它到达那一点我都会得到错误

  

main.cpp:35:60:错误:没有匹配函数来调用' point :: point()'

这是问题区域

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));
      }
};

这里是完整的代码

#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;
}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您没有初始化任何班级edgeList的成员。

嗯,你是,但默认 。这样做需要一个point没有的默认构造函数。因此错误。

edgeList构造函数中的那三行是事后的赋值:

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

这是你应该做的,初始化成员:

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

目前您的代码更像是:

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

如果您的C ++书籍没有解释这一点,那么您需要a better one