Derek Langer
Derek Langer

Reputation: 29

Overload in function on a class type

#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;
}

This is the code I am testing and the issue I am getting is c2661 no overloaded function takes 3 arguments My Header file is: #pragma once

#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();
};

This is the lineType.cpp file that was wanted

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

The error message that appears Error (active) E0289 no instance of constructor "lineType::lineType" matches the argument list Project1 line 20 Same error message for lines 21 and 22 in the source.cpp file. so I am not sure what is occuring?

Upvotes: 0

Views: 117

Answers (1)

Fureeish
Fureeish

Reputation: 13434

lineType::lineType, which is the constructor, is implicitely generated, since you did not provide any user defined constructors. Default-generated constructors take no arguments, yet you try to provide three arguments in lines:

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

I suspect you wanted to take advantage of aggregate initialisation, which you can't unfortunately use, since your a, b and c variables are private. You might want to add such constructor yourself:

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

But that's not all. You have couple more problems with your code. Notably:

if (line1.isParallel(line2)) cout << "line1 is parallel to line 2" << endl;

contains a typo. It should be isParrallel, as declared in your class (which is also a typo) instead of isParallel. Fix either of these.

Lastly, the line:

if (line2.intersect(line3, x, y))

will not compile, since intersect() returns void, not bool. if statements require that they are provided either bools or something that's implicitely convertible to bool type. Make your function return bool, which is the logical assumption for a function that's name starts with is.

Upvotes: 1

Related Questions