2016-03-21 49 views
-3

Merhaba Bir programda hata ayıklamaya çalışıyorum ve aldığım hatalardan biri 'Oluşturucu için Eksik Başlangıç' dır. Vektör açılışını bildirmem gerekiyor mu ve nasıl başlatabilirim?Başlatma için Eksik Oluşturucu

#include <iostream> 
#include <vector> 

using namespace std; 

class Point { 
private: 
    double x; 
    double y; 
public: 
    double get_x() { return x; } 
    double get_y() { return y; } 
    bool set_x(double arg) { 
     x = arg; 
     return true; 
    } 
    bool set_y(double arg) { 
     y = arg; 
     return true; 
    } 
    Point() : x(0), y(0) {} 
    Point(double argx, double argy) : x(argx), y(argy) { 
    } 
}; 


class Vector { 
private: 
    Point A; 
    Point B; 
public: 
    Point get_A() { return A; } 
    Point get_B() { return B; } 
    Vector(const Point &arg1, const Point &arg2) : A(arg1), B(arg2) 
    { 
     //this->A = arg1; 
     //this->B = arg2; 
     //A = arg1; 
     //B = arg2; 
    } 
    void set_A(const Point &arg) { 
     A = arg; 
    } 
    void set_B(const Point &arg) { 
     B = arg; 
    } 
    static Vector add_vector(const Vector &vector1, const Vector &vector2) { 
     if (&vector1.B != &vector2.A) { 

      //Error 1 Vector V1 No Matching constructor for initialization for 'vector' 

      Vector rval; 
      return rval; 
     } 

     Point one = vector1.A; 
     Point two = vector2.B; 

     Vector newvector(one, two); 
     //newvector.A = one; 
     //newvector.B = two; 
     return newvector; 

    } 
    Vector add_vector(const Vector &arg) { 
     // Type of this? Vector *; These three lines are equivalent: 
     //Point one = this->A; 
     //Point one = (*this).A; 
     Point one = A; 

     Point two = arg.B; 

     Vector newvector(one, two); 
     //newvector.A = one; 
     //newvector.B = two; 
     return newvector; 
    } 

}; 


int main() { 

    //Error 2 Vector v No Matching constructor for initialization for 'vector' 


    Vector v; 
    cout << "(" << v.get_A().get_x() << ", " << v.get_A().get_y() << "),\n" << 
    "(" << v.get_B().get_x() << ", " << v.get_B().get_y() << ")\n"; 

    //Error 3 Vector V1 No Matching constructor for initialization for 'vector' 


    Vector v1(1,2), v2(2,3); 
    Vector res = Vector::add_vector(v1, v2); 
    cout << "(" << res.get_A().get_x() << ", " << res.get_A().get_y() << "),\n" << 
    "(" << res.get_B().get_x() << ", " << res.get_B().get_y() << ")\n"; 

} 

cevap

1

Sorununuz, burada sınıfınızın varsayılan yapılandırılabilir değil.

Vector rval; 

Varsayılan kurucu gerektirir. Kullanıcı tanımlı bir kurucu sağladığınız için derleyici artık sizin için varsayılan bir kurucu yapamaz.

C++ 11 veya daha varsa

Vector() = default; 

kullanabilirsiniz Vector için varsayılan bir kurucu oluşturmak için veya önceden C++ 11 için

Vector() {} 

kullanabilirsiniz.

İkinizin Point s ihtiyacı vardır ve her Point ihtiyaçları 2 değer

Vector v1(1,2) 

Vector yapmak için çalışıyoruz ne emin değilim.

+0

Teşekkürler, Vector v1 (1,2) ile ilgili olarak bu programın bir ödev için hata ayıklaması yapmam gerekiyor. Bir kısmı bunun sorulmasını istiyor buna ihtiyacım var ve ne işe yarıyor ya da ne yapmak istiyor. Teşekkürler @NathanOliver – Bigboy6

+0

@ Bigboy6 Emin değilim. 'Vector'' Point's''' 1'' bir 'Point' değil. Yapması gereken kodun kim olduğunu bilmeden, sadece tahmin edebiliyordum. – NathanOliver