错误:与"operator>>"不匹配在'std :: cin>>中停止 [英] error: no match for 'operator>>' in 'std::cin >> stopat'

查看:98
本文介绍了错误:与"operator>>"不匹配在'std :: cin>>中停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图重新使用C ++,这是我很长时间以来的第二个程序了.一切都编译成桃红色,直到到达cin >> stopat;并返回似乎很常见的错误为止:error: no match for 'operator>>' in 'std::cin >> stopat' 我已经看过几件事来解释造成这种情况的原因,但是我并没有真正理解(由于我相对缺乏编程经验).导致此错误的原因是什么?如果再次遇到该错误,该如何解决?

I'm trying to get back into C++, and this is my second program in a long while. Everything compiles just peachy, until it gets to cin >> stopat; where it returns what seems to be a fairly common error: error: no match for 'operator>>' in 'std::cin >> stopat' I've looked through a few things explaining what causes this, but nothing I actually understand (due to my relative inexperience in programming). What causes this error, and how do I fix it in case I come across it again?

#include <iostream>
#include "BigInteger.hh"

using namespace std;

int main()
{
    BigInteger A = 0;
    BigInteger  B = 1;
    BigInteger C = 1;
    BigInteger D = 1;
    BigInteger stop = 1;
    cout << "How Many steps? ";
    BigInteger stopat = 0;
    while (stop != stopat)
    {
        if (stopat == 0)
        {
            cin >> stopat;
            cout << endl << "1" << endl;
        }
        D = C;
        C = A + B;
        cout << C << endl;
        A = C;
        B = D;
        stop = stop + 1;
    }
    cin.get();
}

不知何故,我不认为要链接所引用的库.它们是: https://mattmccutchen.net/bigint/

Somehow, I didn't think to link the libraries referenced. Here they are: https://mattmccutchen.net/bigint/

推荐答案

您尚未向我们展示BigInteger的代码,但需要定义一个函数(在BigInteger.hh或您自己的代码中),例如这个:

You haven't shown us the code for BigInteger, but there would need to be a function defined (either in BigInteger.hh or in your own code) like this:

std::istream& operator >>(std::istream&, BigInteger&);

需要实现此功能,以实际从流中获取单词",然后尝试将其转换为BigInteger.如果幸运的话,BigInteger将有一个采用字符串的构造函数,在这种情况下,它将是这样的:

This function would need to be implemented to actually get a "word" from a stream and try to convert it to a BigInteger. If you're lucky, BigInteger will have a constructor that takes a string, in which case it would be like this:

std::istream& operator >>(std::istream& stream, BigInteger& value)
{
    std::string word;
    if (stream >> word)
        value = BigInteger(word);
}

现在,您已经指出了正在使用的库,您可以执行以下操作.库本身可能应该为您执行此操作,因为它提供了相应的ostream运算符,但是如果您调查一下,就会发现通用的,库质量的流运算符比我在这里写的要复杂.

Now that you have pointed out the library that's being used, here's what you can do. The library itself should probably do this for you, since it provides the corresponding ostream operator, but if you look into that you will see that general-purpose, library-quality stream operators are more complex than what I'm writing here.

#include <BigIntegerUtils.hh>

std::istream& operator >>(std::istream& stream, BigInteger& value)
{
    std::string word;
    if (stream >> word)
        value = stringToBigInteger(word);
}

这篇关于错误:与"operator&gt;&gt;"不匹配在'std :: cin&gt;&gt;中停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆