gpt4 book ai didi

c++ - 无法在 C++ 中使用类实例

转载 作者:行者123 更新时间:2023-11-30 03:59:39 25 4
gpt4 key购买 nike

我正在尝试创建一个类来处理一些将解析输入的函数,因此我必须在 main() 中创建一个实例,如下所示:

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

但是,我收到以下错误 triangle.cpp|37|error: no matching function for call to 'Triangle::exists()'|

最佳答案

你在调用它时没有参数。

if(!t.exists())

但是你声明为:-

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

在类似的行上,您的构造函数调用与其声明不匹配...

关于c++ - 无法在 C++ 中使用类实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26833073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com