operator overloading.

Hi, I'm trying to overload an operator to accept a value of data type Rock, which is an enumerated data type I have defined.

1
2
3
4
5
enum Rock {BASALT = 0, DOLOMITE = 1, GRANITE = 2, GYPSUM = 3, LIMESTONE = 4,
		   MARBLE = 5, OBSIDIAN = 6, QUARTZITE = 7, SANDSTONE = 8, SHALE = 9,
		   ROCK_OVERFLOW = 10}; 

std::istream & operator>>(istream & in, Rock & rockVal);


Also, I have, which does not seem to be working:
1
2
3
4
5
6
istream & operator>>(istream &in, Rock &rockVal) {
	
	in >> rockVal; 
	return in; 
	
} 


Im defining a variable like so: Rock sample; and then Im trying to use cin>> sample; to input BASALT or another rock type, and its not working. I'm getting a segmentation fault for some reason. I'm new to this so much help is appreciated! I basically want to input anytime and anyform like so: BASALT, basalt, Basalt, etc.. into my sample variable, and I want the operator to convert it for me.
Your function is infinitely recursive. This is what the compiler sees:
1
2
3
4
T1 &right_shift(T1 &a,T2 &b){
    right_shift(a,b);
    return a;
}
Last edited on
Ok, Im really lost then. I need to try something like this, hopefully you can get the right idea, I'm not sure how to show this.

1
2
3
4
5
6
7
8
istream & operator>>(istream &in, Rock &rockVal) {
	
	if(rockVal == BASALT) { 
		in >> "BASALT"; 
	} 
	return in; 
	
} 
You need to read a string, then convert the string to the appropriate value.

1
2
3
4
5
6
7
8
9
10
11
std::istream& operator>>( std::istream& is, Rock& rock ) {
    std::string str;
    is >> str;
    if( strcasecmp( str.str(), "basalt" ) ) {
        rock = BASALT;
        return is;
    } else if( /* ... */ ) {
    } else {
        // Didn't read anything valid; set the fail bit on is and return.
    }
}

Topic archived. No new replies allowed.