overloading cin and cout

getting the following error message

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [8]' (or there is no acceptable conversion)

////////////////////////////////////////////////////////////////////////

//RecyclingMachine.h
using std::istream;

using namespace std;

class RecyclingMachine
{

public:
//operators
friend istream & operator>>(istream& , RecyclingMachine& );
friend ostream & operator<<(ostream& , const RecyclingMachine& );


RecyclingMachine();//default constructor
RecyclingMachine(string,int,double);


//mutators
void set_containerType(string);
void set_diameter(double);
void set_numContainers(int);

//accessors
string get_containerType() const;
double get_diameter()const;
int get_numContainers()const;
void printrefund();

private://member variables
string m_containerType;//plastic or glass or aluminum
int m_numContainers;//how many containers
double m_diameter;//container size inches



};

#endif


//////////////////////////////////////////////////////////////////////////////

//RecyclingMachine.cpp
#include <iostream>
#include<string>
#include "RecyclingMachine.h"
using std::ostream;
using std::istream;
using namespace std;

//public constructors
RecyclingMachine::RecyclingMachine()
{
set_containerType("");
set_numContainers(0);
set_diameter(0);
}

RecyclingMachine::RecyclingMachine(string type, int numbercont, double size)
{
set_containerType(type);
set_numContainers(numbercont);
set_diameter(size);
}

//Public. operators
istream & operator >> (istream &is, RecyclingMachine &aRecMac)
{

string contType;
int numCont;
double diameter;

is >> contType >> numCont >> diameter; //("plastic";,5,5)
aRecMac.set_containerType(contType); //string
aRecMac.set_numContainers(numCont);//int
aRecMac.set_diameter(diameter);//double
return is;
}


ostream & operator << (ostream &os, const RecyclingMachine &aRecMac)
{
os << aRecMac.get_containerType() <<" container, "<< aRecMac.get_numContainers() <<" Total containers, and "<<
aRecMac.get_diameter() <<" inches. "<<endl;
return os;
}





//public. member functions
void RecyclingMachine::set_containerType(string type)
{
if( type == "plastic" )
{
m_containerType= type;
cout<<" You inserted plastic bottle. Sent to plastic bin."<<endl;
}
else if( type == "aluminum" )
{
m_containerType= type;
cout<<" You inserted alumminum can. Sent to aluminum bin. "<<endl;
}
else if( type == "glass" )
{
m_containerType= type;
cout<<" You inserted glass bottle. Sent to glass bin. "<<endl;
}
else if(type == "")
{
cout<<" Please manually set container type. "<<endl;
}
else
cout<<" Recycling center only accepts glass, plastic, or aluminum. "<<endl;
}

void RecyclingMachine::set_diameter(double size)
{

if(size==5.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==3.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==2.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==0)
{
cout<<" Please manually enter the size of container. "<<endl;
}
else
{
cout<<" Machine cannot accept this size. "<<endl;
}
}

void RecyclingMachine::set_numContainers(int numbercont)
{
if(numbercont > 0)
{
m_numContainers=numbercont;
cout<<" You have inserted "<<m_numContainers<<" containers "<<endl;
}
else if(numbercont == 0)
{
cout<<" Please enter number of containers to be recycled. "<<endl;
}
}

void RecyclingMachine::printrefund()
{
get_numContainers();
get_diameter();
if(get_numContainers() > 0)
{
cout<<" Your deposit refund is "<<"$"<<get_numContainers()*.10<<endl;
}
else
cout<<" Please enter containers for deposit refund. "<<endl;
}

string RecyclingMachine::get_containerType() const
{
return m_containerType;
}

double RecyclingMachine::get_diameter() const
{
return m_diameter;
}

int RecyclingMachine::get_numContainers() const
{
return m_numContainers;
}


///////////////////////////////////////////////////////////////////////////

/*_____________________
Daniel Stewart
homework 6
creating class part 2
_____________________*/
//Main Routine

#include <iostream>
#include "RecyclingMachine.h"
using namespace std;
#include <string>
using std::ostream;
using std::istream;

int main()
{
RecyclingMachine mymachine1

cin >> "plastic" >> 5 >> 5 ;


cout << mymachine1;


system("pause");
return 0;
}

In your main function you haven't delcared any variables to accept the input. You've declared a object of type 'RecyclingMachine' but you haven't stated that your putting your values into that object e.g.

cin >> mymachine1
I created the variables in the main routine. But I dont think the main is calling the >> operator function. Am I supposed to somehow set mymachine1 equal to these variables, and then do cin>>mymachine1.
I think i might be on the wrong track . Thanks for the help


//Main Routine

#include <iostream>
#include "RecyclingMachine.h"
using namespace std;
#include <string>
using std::ostream;
using std::istream;
#include <iomanip>

int main()
{
RecyclingMachine mymachine1;//(string type, int howmany, double size)

string contType;
int numCont;
double diameter;

contType = "plastic";
numCont = 5;
diameter = 5;

cin >> contType >> numCont >> diameter;


cout << mymachine1 << endl;


system("pause");
return 0;
}


/////////////////////////////////////////////////////////////////////////////////////////////


//RecyclingMachine.h
#include <string>
#include <iostream>
#ifndef RECYCLINGMACHINE_H
#define RECYCLINGMACHINE_H
using std::ostream;
using std::istream;

using namespace std;

class RecyclingMachine
{

public:
//operators
friend istream & operator>>(istream& , RecyclingMachine& aRecMac);
friend ostream & operator<<(ostream& , const RecyclingMachine& aRecMac );


RecyclingMachine();//default constructor
RecyclingMachine(string,int,double);


//mutators
void set_containerType(string);
void set_diameter(double);
void set_numContainers(int);

//accessors
string get_containerType() const;
double get_diameter()const;
int get_numContainers()const;
void printrefund();

private://member variables
string m_containerType;//plastic or glass or aluminum
int m_numContainers;//how many containers
double m_diameter;//container size inches



};

#endif


/////////////////////////////////////////////////////////////////////////////////////////////

//RecyclingMachine.cpp
#include <iostream>
#include<string>
#include "RecyclingMachine.h"
using std::ostream;
using std::istream;
using namespace std;

//constructors
RecyclingMachine::RecyclingMachine()//Default constructor
{
set_containerType("" );
set_numContainers(0);
set_diameter(0);
}

RecyclingMachine::RecyclingMachine(string type, int numbercont, double size)
{
set_containerType(type);
set_numContainers(numbercont);
set_diameter(size);
}

//Public. operators
istream & operator >> (istream &is, RecyclingMachine &aRecMac)
{

string contType;
int numCont;
double diameter;

is >> contType >> numCont >> diameter; //("plastic",5,5)

aRecMac.set_containerType(contType); //string
aRecMac.set_numContainers(numCont);//int
aRecMac.set_diameter(diameter);//double
return is;
}


ostream & operator << (ostream &os, const RecyclingMachine &aRecMac)
{
os << aRecMac.get_containerType() <<" container, "<< aRecMac.get_numContainers() <<" Total containers, and "<<
aRecMac.get_diameter() <<" inches. "<<endl;
return os;
}





//public. member functions
void RecyclingMachine::set_containerType(string type)
{
if( type == "plastic" )
{
m_containerType= type;
cout<<" You inserted plastic bottle. Sent to plastic bin."<<endl;
}
else if( type == "aluminum" )
{
m_containerType= type;
cout<<" You inserted alumminum can. Sent to aluminum bin. "<<endl;
}
else if( type == "glass" )
{
m_containerType= type;
cout<<" You inserted glass bottle. Sent to glass bin. "<<endl;
}
else if(type == "")
{
cout<<" Please manually set container type. "<<endl;
}
else
cout<<" Recycling center only accepts glass, plastic, or aluminum. "<<endl;
}

void RecyclingMachine::set_diameter(double size)
{

if(size==5.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==3.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==2.0)
{
m_diameter=size;
cout<<" Machine has been set to container diameter of "<<m_diameter<<
" inches. "<<endl;
}
else if(size==0)
{
cout<<" Please manually enter the size of container. "<<endl;
}
else
{
cout<<" Machine cannot accept this size. "<<endl;
}
}

void RecyclingMachine::set_numContainers(int numbercont)
{
if(numbercont > 0)
{
m_numContainers=numbercont;
cout<<" You have inserted "<<m_numContainers<<" containers "<<endl;
}
else if(numbercont == 0)
{
cout<<" Please enter number of containers to be recycled. "<<endl;
}
}

void RecyclingMachine::printrefund()
{
get_numContainers();
get_diameter();
if(get_numContainers() > 0)
{
cout<<" Your deposit refund is "<<"$"<<get_numContainers()*.10<<endl;
}
else
cout<<" Please enter containers for deposit refund. "<<endl;
}

string RecyclingMachine::get_containerType() const
{
return m_containerType;
}

double RecyclingMachine::get_diameter() const
{
return m_diameter;
}

int RecyclingMachine::get_numContainers() const
{
return m_numContainers;
}
Oh right right, once you enter the variables you initialize my machine1 with them e.g.

mymachine1(contType, numCont, diameter);

This will initialize your object using the input you took, but this isn't what you want since you overloaded the operator>>, you want to be able to input a RecyclingMachine object or so I assume?

Also: please add the
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 brace around your code, its killing my eyes.

Ok heres what I suppose you could do in main.

Ex 1:
[code]RecyclingMachine machina;

string name = "";
int nom = 0;
double dub = 0;

cin >> name >> nom >> dub;

machina(name,nom,dub);

cout << machina << endl;


Ex 2
1
2
3
4
5
RecyclingMachine machina;

cin >> machina; // This should work since you've overloaded the >>

cout << machina << endl;


Try these and tell me what happens. And you don't need to repost the entire code every single time, just what you've changed.
Topic archived. No new replies allowed.