I had a headache to compile the programm:
// englio.cpp
// overloaded << and >> operators
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) //constructor (no args)
{ }
//constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
friend istream& operator >> (istream& s, Distance& d);
friend ostream& operator << (ostream& s, Distance& d);
};
//--------------------------------------------------------------
istream & operator >> (istream& s, Distance& d) //get Distance
{ //from user
cout << "\nEnter feet: "; s >> d.feet; //using
cout << "Enter inches: "; s >> d.inches; //overloaded
return s; //>> operator
}
//--------------------------------------------------------------
ostream& operator << (ostream& s, Distance& d) //display
{ //Distance
s << d.feet << "\'-" << d.inches << '\"'; //using
return s; //overloaded
} //<< operator
////////////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist2; //define Distances
Distance dist3(11, 6.25); //define, initialize dist3
cout << "\nEnter two Distance values:";
cin >> dist1 >> dist2; //get values from user
//display distances
cout << "\ndist1 = " << dist1 << "\ndist2 = " << dist2;
cout << "\ndist3 = " << dist3 << endl;
return 0;
}
It produces the following errors:
------------------Configuration: Englio - Win32 Debug--------------------
Compiling...
Englio.cpp
H:\home\vc\test\lab04\Englio.cpp(23) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(24) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(40) : error C2593: 'operator >>' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(42) : error C2593: 'operator <<' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(43) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
Englio.obj - 7 error(s), 0 warning(s)
I appreciate whom tell me if the result is different.
// englio.cpp
// overloaded << and >> operators
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) //constructor (no args)
{ }
//constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{ }
friend istream& operator >> (istream& s, Distance& d);
friend ostream& operator << (ostream& s, Distance& d);
};
//--------------------------------------------------------------
istream & operator >> (istream& s, Distance& d) //get Distance
{ //from user
cout << "\nEnter feet: "; s >> d.feet; //using
cout << "Enter inches: "; s >> d.inches; //overloaded
return s; //>> operator
}
//--------------------------------------------------------------
ostream& operator << (ostream& s, Distance& d) //display
{ //Distance
s << d.feet << "\'-" << d.inches << '\"'; //using
return s; //overloaded
} //<< operator
////////////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist2; //define Distances
Distance dist3(11, 6.25); //define, initialize dist3
cout << "\nEnter two Distance values:";
cin >> dist1 >> dist2; //get values from user
//display distances
cout << "\ndist1 = " << dist1 << "\ndist2 = " << dist2;
cout << "\ndist3 = " << dist3 << endl;
return 0;
}
It produces the following errors:
------------------Configuration: Englio - Win32 Debug--------------------
Compiling...
Englio.cpp
H:\home\vc\test\lab04\Englio.cpp(23) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(24) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'feet' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(9) : see declaration of 'feet'
H:\home\vc\test\lab04\Englio.cpp(30) : error C2248: 'inches' : cannot access private member declared in class 'Distance'
H:\home\vc\test\lab04\Englio.cpp(10) : see declaration of 'inches'
H:\home\vc\test\lab04\Englio.cpp(40) : error C2593: 'operator >>' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(42) : error C2593: 'operator <<' is ambiguous
H:\home\vc\test\lab04\Englio.cpp(43) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
Englio.obj - 7 error(s), 0 warning(s)
I appreciate whom tell me if the result is different.