在使用另一个类的类中重载operator ==

时间:2015-10-12 00:24:10

标签: c++ segmentation-fault operator-overloading

我开始学习C ++和面向对象的编程,我很难理解我的作业有什么问题。 我有两个类:点和线来定义笛卡尔计划中的点和线。我正在尝试为行类重载==运算符。

point.h中类的定义:

#ifndef POINT_H
#define POINT_H

#include <iostream>

class point
{
    public:

        // Default constructor
        // Initializes (x,y) = (0,0)
        point();

        // Overloaded constructor
        point(double , double);

        // Copy constructor
        point(const point& );

        // Sets new value for x
        void set_x (double );

        // Sets new value for y
        void set_y(double );

        // Returns value of x
        double get_x();

        // Returns value of y
        double get_y();

        // Overloads = operator
        void operator = (const point& );

    private:
        double x, y;

};

// Overloads == operator
bool operator ==(point& , point& );

#endif

在point.cpp中实现

#include <iostream>
#include "point.h"

using namespace std;

// Default constructor
// Initializes (x,y) = (0,0)
point::point() {
    x = 0;
    y = 0;
}

// Overloaded constructor
point::point(double X, double Y) {
    x = X;
    y = Y;
}

// Copy constructor
point::point(const point& p) {
    this->x = p.x;
    this->y = p.y;
}

// Sets new value for x
void point::set_x (double X) {
    x = X;
}

// Sets new value for y
void point::set_y(double Y) {
    y = Y;
}

// Returns value of x
double point::get_x() {
    return x;
}

// Returns value of y
double point::get_y() {
    return y;
}

// Overloads = operator
void point::operator = (const point& source) {
    this->x = source.x;
    this->y = source.y;
}

// Overloads == operator
bool operator == (point& p1, point& p2) {
    if(p1.get_x() == p2.get_x() && p1.get_y() == p2.get_y()) {
        return true;
    }
    return false;
}

line.h文件:

#ifndef LINE_H
#define LINE_H

#include <iostream>
#include "point.h"

class line {
    public:

        // line = X axis
        line();

        // line passes through (0,0) and
        // the point provided by programmer
        line(point );

        // line passes through the 2
        // points provided
        line(point , point );

        // Copy Constructor
        line(line& );

        // Overloads = operator
        void operator = (line& );

        // Overloads == operator
        friend bool operator ==(const line& , const line& );


    private:
        point p1;
        point p2;
};

#endif

line.cpp:

#include <iostream>
#include "point.h"
#include "line.h"

using namespace std;

// line = X axis
line::line() {
    p1.set_x(0);
    p1.set_y(0);
    p2.set_x(100);
    p2.set_y(0);
}

// line passes through (0,0) and point p
line::line(point p) {
    p1.set_x(0);
    p1.set_y(0);
    p2.set_x(p.get_x());
    p2.set_y(p.get_y());
}

// line passes through the 2 points p & q
line::line(point p, point q) {
    p1.set_x(p.get_x());
    p1.set_y(p.get_y());
    p2.set_x(q.get_x());
    p2.set_y(q.get_y());
}

// Copy Constructor
line::line(line& L) {
    this->p1 = L.p1;
    this->p2 = L.p2;
}

// Overloads = operator
void line::operator = (line& L) {
    this->p1.set_x(L.p1.get_x());
    this->p1.set_y(L.p1.get_y());
    this->p2.set_x(L.p2.get_x());
    this->p2.set_y(L.p2.get_y());
}

bool operator ==(const line& L1, const line& L2) {
    if((L1.p1 == L2.p1) && (L1.p2 == L2.p2)) {
        return true;
    }
    return false;
}

行的==运算符不起作用...代码编译时没有任何错误或警告但是当我尝试使用它时...它会给我一条消息:分段错误:11

我用这种方式调用这些方法:

int main (int argc, char const* argv[])
{
    point p1(10, 10), p2(13, 18.5), p3(0,0);
    line l1, l2(p1,p3), l3(p1,p2);
    l1 = l2;

    if(l1 == l2) {
        cout << "l1 == le" << endl;
    }
    else {
        cout << "l1 != l2" << endl;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

修复operator== point。它应该像这样const&

bool operator == (const point& p1, const point& p2)

要解决此问题,您需要在标题

中标记const的getter
    // Returns value of x
    double get_x() const;

    // Returns value of y
    double get_y() const;

和来源

// Returns value of x
double point::get_x() const {
    return x;
}

// Returns value of y
double point::get_y() const {
    return y;
}

这应该可以做到,输出是

  

l1 == le