-3
嗨我想調試一個程序,我收到的錯誤之一是'缺少構造函數的初始化'。我是否需要預先聲明矢量,如何初始化它?缺少構造函數初始化
#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";
}
謝謝,關於Vector v1(1,2)這個程序,我必須爲一個任務進行調試。所以它的一部分是問我是否需要這個,它做了什麼或者它打算做什麼。謝謝@NathanOliver – Bigboy6
@ Bigboy6我不確定。 'Vector'需要'Point's,'1'不是'Point'。不知道編寫代碼的人究竟會做什麼,我只能猜測。 – NathanOliver