简体   繁体   English

重载'>>'运算符时出错

[英]Error while Overloading the '>>' operator

I'm designing a Money object for a project. 我正在为一个项目设计一个Money对象。 I'm not asking for help on implementation, because I really have to figure that out myself, but I'm getting the following error (and this is the only error!) 我不是在寻求实施方面的帮助,因为我必须自己解决这个问题,但是我收到了以下错误(这是唯一的错误!)

error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion) 错误C2678:二进制'>>':找不到哪个运算符带有'std :: istream'类型的左操作数(或者没有可接受的转换)

I don't have errors in my Money.h or Money.cpp file, just the test.cpp file. 我的Money.h或Money.cpp文件中没有错误,只有test.cpp文件。 Here are the contents of all three files: 以下是所有三个文件的内容:

Money.h Money.h

#ifndef MONEY_H
#define MONEY_H
#include <iostream>
#include <string>
class Money
{
public:
    Money( );
    Money( int dollars, int cents );

    friend std::istream& operator>>( std::istream &i, Money &m );

private:
    int dollars;
    int cents;
};
#endif

Money.cpp Money.cpp

#include "Money.h"

Money::Money(void) : dollars(0), cents(0)
{

}

Money::Money( int dollars, int cents ) : dollars(dollars), cents(cents)
{

}

std::istream& operator>>( std::istream &i, Money &m )
{

    int d;
    int c;
    char input;
    std::string dollars = "";
    std::string cents = "";

    input = std::cin.peek();
    while (std::cin.peek() != '.')
    {
        if ( !( (input >= '0') && (input <= '9') ) )
        {
            std::cin.ignore();
        }
        else
        {
            input = std::cin.get();
        }

        dollars += input;
    }

    if ( std::cin.peek() == '.')
    {
        std::cin.ignore();
    }

    std::cin >> cents;

    d = atoi(dollars.c_str());
    c = atoi(cents.c_str());

    m = Money(d, c);

    return i;
}

Finally, test.cpp: 最后,test.cpp:

#include "Money.h"

int main()
{
    Money newMoney();
    std::cout << "Enter a money object!" << std::endl;
    std::cin >> newMoney;
}

So, there you have it. 所以你有它。 I'm pretty sure that's about as trimmed-down as I can get it. 我非常确定这是我可以得到的减少。

You don't have enough data in the question. 您在问题中没有足够的数据。 But, consulting my crystal ball, I see that you have defined operator>> in your .CPP file, but failed to declare operator>> in your .H. 但是,咨询我的水晶球,我看到你已经在.CPP文件中定义了 operator>> ,但未能在你的.H中声明 operator>>

Add the following line to your .H: 将以下行添加到.H:

std::istream& operator>>( std::istream &i, Money &m );

My crystal ball is faulty. 我的水晶球有问题。 The error lies here: 错误在于:

Money newMoney();

That doesn't declare a Money object named newMoney . 这不会声明名为newMoneyMoney对象。 That declares an external function named newMoney that takes no paramaters and returns a Money object. 这声明了一个名为newMoney的外部函数,它不接受参数并返回一个Money对象。 Replace that line with this: 用这个替换该行:

Money newMoney;

Nothing pops out other than normally I would define this 除了通常我会定义这个之外什么都没有弹出

std::istream& operator>>

as

friend std::istream& operator>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM