在类类型上重载函数

时间:2018-11-18 21:58:08

标签: c++ header-files

#include <iostream>
#include <iomanip>
#include <cmath>
#include "lineType.h"
using namespace std;

int main()
{
    double x, y;
    double a = 1.; 
    double b = 0.; 
    double c = 1.; 
    double d = 2.; 
    double e = 0.; 
    double f = 3.; 
    double g = 0.; 
    double h = 4.; 
    double i = -1.;

    lineType line1(a, b, c);
    lineType line2(d, e, f);
    lineType line3(g, h, i);

    cout << "Line 1: ";
    line1.display();

    if (line1.isParallel(line2)) cout << "line1 is parallel to line 2" << endl;
    if (line1.isPerp(line3)) cout << "line 1 is perpendicular to line 3" << endl;

    if (line2.intersect(line3, x, y))
        cout << "The intersection of lines 2 and 3 is at point(" << x << ", " << y << ")" << endl;
    else
        cout << "Lines 2 and 3 do not intersect." << endl;

    return 0;
}

这是我正在测试的代码,我遇到的问题是c2661没有重载函数需要3个参数     我的头文件是:     #pragma一次

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

class lineType
{
private:
    double a;
    double b;
    double c;
public:
    void display() const;
    bool isParallel(const lineType& line) const;
    bool isPerp(const lineType& line) const;
    bool intersect(const lineType& line, double& x, double& y);
    lineType();
    lineType(double a2, double b2, double c2);
    ~lineType();
};

这是需要的lineType.cpp文件

#include "lineType.h"
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void lineType::display() const
{
    cout << a << "x + " << b << "y = " << c << endl;
}

bool lineType::isParallel(const lineType& line) const
{
    if (a == 0 && line.a == 0)
        return 1;
    if (b == 0 && line.b == 0)
        return 1;
    else if (-a / b == -line.a / line.b)
        return 1;
    else
        return 0;
}

bool lineType::isPerp(const lineType& line) const
{
    if (a == 0 && line.b == 0)
        return 1;
    if (b == 0 && line.a == 0)
        return 1;
    else if (-a / b == line.b / line.a)
        return 1;
    else
        return 0;
}

bool lineType::intersect(const lineType& line, double& x, double& y)
{
    if (a == 0)
        x = c / b;
    if (line.a == 0)
        x = line.c / line.b;
    if (b == 0)
        y = c / a;
    if (line.b == 0)
    {
        y = line.c / line.a;
    }
    else
    {
        x = ((a*line.c) - (c*line.a)) / ((b*line.a) - (a*line.b));
        y = ((c*line.b) - (b*line.c)) / ((b*line.a) - (a*line.b));
    }
    if (a == 0 && line.a == 0)
        return 0;
    if (b == 0 && line.b == 0)
        return 0;
    return 1;
}

lineType::lineType()
{
    a = 0;
    b = 0;
    c = 0;
}

lineType::lineType(double a2, double b2, double c2)
{
    a = a2;
    b = b2;
    c = c2;
}


lineType::~lineType()
{
}

出现的错误信息 错误(活动)E0289没有构造函数“ lineType :: lineType”的实例与参数列表Project1第20行匹配 source.cpp文件中第21和22行的错误消息相同。所以我不确定会发生什么?

1 个答案:

答案 0 :(得分:1)

lineType::lineType(即构造函数)是隐式生成的,因为您未提供任何用户定义的构造函数。默认生成的构造函数不带任何参数,但是您尝试在行中提供三个参数:

lineType line1(a, b, c);
lineType line2(d, e, f);
lineType line3(g, h, i);

我怀疑您想利用aggregate initialisation的优势,因为您的abc变量是{{1} }。您可能想自己添加这样的构造函数:

private

但这还不是全部。您的代码还有更多问题。值得注意的是:

lineType(const double a, const double b, const double c)
        :a(a), b(b), c(c) { }

包含错字。它应该是if (line1.isParallel(line2)) cout << "line1 is parallel to line 2" << endl; ,而不是isParrallel,这是在您的类中声明的(也是错字)。修复其中任何一个。

最后一行:

isParallel

将不会编译,因为if (line2.intersect(line3, x, y)) 返回intersect(),而不是voidbool语句要求提供if或隐式转换为bool类型的内容。让您的函数返回bool,这是一个以bool开头的函数的逻辑假设。