error: no match for 'operator>>' in 'std::cin >> stopat'

送分小仙女□ 提交于 2019-12-11 04:14:07

问题


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

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


回答1:


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&);

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

Edit: 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);
}



回答2:


What you've left out here is details about your BigInteger class. In order to read one from an input stream with the >> operator, you need to define operator>> (often called a stream extractor) for your class. That's what the compiler error you're getting means.

Essentially, what you need is a function that looks like this:

std::istream &operator>>(std::istream &is, BigInteger &bigint)
{ 
    // parse your bigint representation there
    return is;
}


来源:https://stackoverflow.com/questions/9220599/error-no-match-for-operator-in-stdcin-stopat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!