dwvaxaz
dwvaxaz

Reputation: 49

Unable to use a class instance in C++

I'm trying to create a class to handle a few functions that will parse input and therefore I had to create an instance in main() like so:

#include <iostream>
#include <string>

using namespace std;

class Triangle{

private:
    double a, b, c, h;

public:
    Triangle(double sideA, double sideB, double sideC): a(sideA), b(sideB), c(sideC){}

    double get_perimeter()
    {
        return a + b + c;
    }

    double get_area()
    {
        return (b*h)/2;
    }

    bool exists(double a, double b, double c)
    {
        return a + b > c && a + c > b && b + c > a;
    }
};

int main()
{
    double a, b, c, h;
    cin >> a >> b >> c >> h;

    Triangle t(a, b, c, h);

    if(!t.exists())
    {
        cout << "No such triangle!" << endl;
        return 1;
    }

    cout << t.get_perimeter() << endl;
    cout << t.get_area() << endl;

    return 0;
}

However, I get the following error triangle.cpp|37|error: no matching function for call to 'Triangle::exists()'|

Upvotes: 1

Views: 75

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310920

Member function exists is declared as having three parameters

bool exists(double a, double b, double c)

However you call it without arguments

if(!t.exists())

So the compiler is unable to find function exists declared without parameters.

You should define the function like

bool exists() const
{
    return a + b > c && a + c > b && b + c > a;
}

because it seems a, b, and c are data members of a class instance.

Also you declared constructor as having three parameters

Triangle(double sideA, double sideB, double sideC): /*...*/

However you call it with four arguments

Triangle t(a, b, c, h);

I think that the value of h should be calculated based on values of a, b, and c instead of be entered.

Upvotes: 0

ravi
ravi

Reputation: 10733

You are calling it without parameter.

if(!t.exists())

However you have declaration as :-

bool exists(double a, double b, double c)

On similar lines your constructor call is not matching with its declaration...

Upvotes: 2

Related Questions