2017-05-03 6 views
0

したがって、問題は35行目(edgeListコンストラクターを呼び出します)で発生します。それはそのポイントに到達するたびに、私はエラーここオブジェクトをクラスに渡し、一致する関数エラーを受け取らない

main.cpp:35:60: error: no matching function for call to '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; 
} 

は、私がここで間違ってやっている何を得ますか?

+0

あなたは 'point'クラスに' point() 'コンストラクタを実装していません –

+0

@RajeevSingh:コメントではありません –

答えて

0

あなたのクラスedgeListのメンバーは初期化されていません。

まあ、ありますが、はデフォルトでです。それを行うには、デフォルトのコンストラクタが必要です。これはpointにはありません。したがって、エラー。 edgeListコンストラクタで

これらの3本のラインは、事後の割り当てです:

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

現在、あなたのコードがありますmore:

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が必要です!

+0

ありがとうございました! – David

関連する問題