0

When I compile I get this error message for istream
error: no match for 'operator>>' in 'in >> ((Rock*)this)->Rock::myName'
I did not include the program with main because I have not used it yet.

#ifndef ROCK_H
#define ROCK_H
#include<iostream>
using namespace std;
enum RockName {BASALT, DOLOMITE, GRANITE, GYPSUM, LIMESTONE, MARBLE, OBSIDIAN,
	QUARZITE, SANDSTONE, SHALE, ROCK_OVERFLOW};

class Rock
{
public:
	Rock();
	Rock(RockName rockName); 
	void display(ostream & out) const;
	void read(istream & in);
private:
	RockName myName;
};
ostream & operator <<(ostream & out, const Rock &t);
istream & operator >>(istream & in, Rock &t);
#endif

#include <iostream>
using namespace std;

#include "Rock.h"

Rock::Rock():myName(BASALT)
{
}
Rock::Rock(RockName rockName)
{
myName= rockName;
}
void Rock::display(ostream & out) const
{
	out<<myName;
}
void Rock::read(istream &in)
{
	in>>myName;//this is where I am getting the error
}
ostream & operator<<(ostream & out, const Rock & t)
{
	t.display(out);
	return out;
}
istream & operator>>(istream & in, Rock & t)
{
	t.read(in);
	return in;
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by r.stiltskin
0

The error occurs because the << operator is not overloaded to handle your user-defined type Rock, i.e. istream doesn't know how to read in one of your enum elements.

The real problem is that this doesn't seem to be an appropriate use of an enum. Why are you using enum? Your enum elements BASALT, DOLOMITE, etc, are not equivalent to the strings "BASALT", "DOLOMITE", ... Instead, they are essentially "nicknames" for the integers 0, 1, 2,..., 10. If your purpose is to ensure that the user enters a valid string, this is not the way to do it. You should create an array (or better yet, an STL set) containing your valid strings, and a function to test and verify that the string that the user enters matches an element of that array or set.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.