Extraction operator overloading confusion!

Hi all!

I have been stuck on extraction overloading for hours now and the error code i get is "All paths through this function will call itself" if that helps!

1
2
3
4
5
istream& operator>>( istream& is, const Measurement& content){
    
    is >> content.feet >> content.inch;
    return is;
}


This is my extraction overload header and I have two private variable 'feet' and 'inch' of type int.

1
2
3
4
5
6
7
8
9
10
11
Measurement::Measurement(int x){
    setFeet(x);
    setInch(x);
}

Measurement::Measurement(Measurement& x){ //Copy constructor
    feet = x.feet;
    inch = x.inch;
}

Measurement::Measurement(){} //Empty parameter constructor 

Here are my constructors.

The line in my main.cpp is simply 'cin << obj;'

Any hints?
Thank you!
Last edited on
I assume you mean "cin >> obj;".

Why don't you tell us where the error says its happening? The more information you can provide us, the better chance we have of helping you.

I don't see anything immediately wrong with what you have provided. Perhaps post more context?
Last edited on
"All paths through this function will call itself"

I can't see why. Are you sure that error comes from this part of the code?

Anyways, you can't modify (the referent of) content because it's a reference to const. Get rid of it:

1
2
istream& operator>>( istream& is, /* const */ Measurement& content) 
{ return is >> content.feet >> content.inch; } 


The line in my main.cpp is simply 'cin << obj;'
Ought to be cin >> obj.

The arrow points in the direction the data moves - in this case, from std::cin into obj.

For example:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

struct measurement {
  int feet, inch;
};

std::istream &operator>>(std::istream &s, measurement &m) {
  return s >> m.feet >> m.inch;
}

int main() { measurement obj; std::cin >> obj; }


Also, your copy constructor shouldn't be there: best practice is to not write special member functions yourself unless they are required; they are only required when the class directly manages a resource - memory, for example. This is called the rule of zero:
http://en.cppreference.com/w/cpp/language/rule_of_three

It also really should have
 
Measurement::Measurement(Measurement const & x)
so that you can copy const objects.



Last edited on
Thanks for the reply guys! Sorry about the late response.

I actually experimented with the header after I posted my post and actually solved it.

I took out the 'const' in the extraction operator header and it worked!!

So i looks like this,

1
2
3
4
5
istream& operator>>( istream& is, Measurement& content){
    
    is >> content.feet >> content.inch;
    return is;
}


Now, I don't understand why that works though since I am still confused about the purpose of 'const'.
Last edited on
const means that it's an object that cannot be modified after being initially set. You're trying to put information into content.feet and content.inch, which are part of your content object; this cannot be done if content is const.
Topic archived. No new replies allowed.