C++试图通过getline函数为函数调用获取用户输入值

标签 c++ class cmath

我正在尝试使用类来创建一个程序,该程序通过用户输入可以执行勾股定理的操作,但我收到此错误:

错误(事件)E0304 没有重载函数“getline”的实例与参数列表匹配

这是我的代码:

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

class PythagoreanTheorum
{

public:

    double a;
    double b;
    double c;

    void seta(double A)
    {
        a = A;
    }
    void setb(double B)
    {
        b = B;
    }

    void setc(double C)
    {
        c = C;
    }

    double calcAreea()
    {
        return a * pow(a, 2) + b * pow(b, 2) == c * pow(c, 2);
    }    

};


int main()
{
   //Define one right triangles

        PythagoreanTheorum righttriangle1;

    double a;
    double b;
    double c;
    cout << "Enter the value for a: " << endl;
    righttriangle1.a = getline(cin, a);

    cout << "Enter the value for b: " << endl;
    righttriangle1.b = getline(cin, b);


    cout << "Enter the value for c: " << endl;
    righttriangle1.c = getline(cin, c);





}

最佳答案

std::getline 读取字符串,而不是 double 数。所以你必须阅读 std::string 然后将其转换为 double (与 stod )。

您可以改用 >>输入运算符:

cout << "Enter the value for a: " << endl;
std::cin >> righttriangle1.a;

cout << "Enter the value for b: " << endl;
std::cin >> righttriangle1.b;

cout << "Enter the value for c: " << endl;
std::cin >> righttriangle1.c;

关于C++试图通过getline函数为函数调用获取用户输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917629/

相关文章:

c++ - 使用 C++ 函数指针作为信号处理程序。

c++ - 计算装配错误率的工具

c++ - 一次加和乘 x*y+z 的 cmath 函数

python scipy lesssq 适合复数

c++ - 模板 `inline` 函数的静态局部变量

c++ - 为什么 `QDateTime`实现隐式共享?

javascript - 如何让函数在创建时调用自己的函数

java - CompareTo() Java 快速问题

c++ - 错误 C2512 'DerivedClass' : no appropriate default constructor available

c++ - 仅使用库中的某些功能?