>" I am creating a Money object for a project. I am not asking for help with the implementation because I re...">

Error when overloading operator ">>"

I am creating a Money object for a project. I am not asking for help with the implementation because I really need to figure it out, but I am getting the following error (and this is the only error!)

error C2678: binary '→': no ​​operator found that accepts a left operand of type 'std :: istream' (or no acceptable conversion)

I have no errors in Money.h or Money.cpp, just in test.cpp. Here are the contents of all three files:


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

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

    int dollars;
    int cents;



#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') ) )
            input = std::cin.get();

        dollars += input;

    if ( std::cin.peek() == '.')

    std::cin >> cents;

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

    m = Money(d, c);

    return i;


Finally, test.cpp:

#include "Money.h"

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


So this is it. I'm pretty sure I'm cropping as soon as I can.


source to share

2 answers

You don't have enough data in the question. But consulting my crystal ball, I can see that you defined operator>>

in your .CPP file but failed to declare operator>>

in your .H.

Add the following line to your .H:

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


My crystal ball is defective. The error is here:

Money newMoney();


This does not declare a Money

named object newMoney

. This declares a named external function newMoney

that takes no parameters and returns an object Money

. Replace this line with the following:

Money newMoney;




Nothing will pop out except as usual, I would define it

std::istream& operator>>



friend std::istream& operator>>




All Articles