Nov 12, 2020 at 6:49am UTC
I am getting an error in vs c++ compilor showing no match for << operator error in c++, followed by lots of unknown errors,,,
Here's the code :
#include<iostream>
#include<stdlib.h>
using namespace std;
class Distance
{
public:
int feet;
float inches;
public:
Distance():feet(0),inches(0.0)
{ }
Distance(int f,float i):feet(f),inches(i)
{ }
void get_dist()
{
cout<<"Enter feet : ";
cin>>feet;
cout<<endl;
cout<<"Enter inches : ";
cin>>inches;
cout<<endl;
}
void display() const
{
cout<<feet<<"\'"<<inches<<'\"'<<endl;
}
friend ostream& operator << (ostream &theStream, Distance &P);
friend istream& operator >> (istream &Stream , Distance &Q);
};
ostream& operator << (ostream &theStream, Distance &P)
{
theStream<<"feet : "<<P.feet<<endl;
theStream<<"inches : "<<P.inches<<endl;
return theStream;
}
istream& operator >> (istream &Stream , Distance &Q)
{
Stream>>Q.feet>>Q.inches;
return Stream;
}
class Distsign : public Distance
{
char sign;
public:
Distsign():Distance()
{
sign='+';
}
Distsign(int f,float i,char sg):Distance(f,i)
{
sg='+';
sign=sg;
}
void get_dist()
{
Distance::get_dist();
char ch;
cout<<"Enter sign(+ or -) : \n";
cin>>ch;
if(ch=='+')
{
sign='+';
}
else
{
sign='-';
}
}
void display() const
{
cout<<sign;
Distance::display();
}
};
int main()
{
system("cls");
Distsign a;
a.get_dist();
Distsign b(11,6.25,'+');
Distsign g(100,5.5,'-');
cout<<"\n Alpha = "<<a.display()<<endl;
cout<<"\n Beta = "<<b.display()<<endl;
cout<<"\n Gamma = "<<g.display()<<endl;
return 0;
}
Last edited on Nov 12, 2020 at 6:50am UTC
Nov 12, 2020 at 6:52am UTC
You need to use code tags when posting code so that it retains indentation.
[code]
your code here
[/code]
Last edited on Nov 12, 2020 at 7:08am UTC
Nov 12, 2020 at 7:30am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#include<iostream>
#include<stdlib.h>
using namespace std;
class Distance
{
public :
int feet;
float inches;
public :
Distance():feet(0),inches(0.0)
{ }
Distance(int f,float i):feet(f),inches(i)
{ }
void get_dist()
{
cout<<"Enter feet : " ;
cin>>feet;
cout<<endl;
cout<<"Enter inches : " ;
cin>>inches;
cout<<endl;
}
void display() const
{
cout<<feet<<"\'" <<inches<<'\"' <<endl;
}
friend ostream& operator << (ostream &theStream, Distance &P);
friend istream& operator >> (istream &Stream , Distance &Q);
};
ostream& operator << (ostream &theStream, Distance &P)
{
theStream<<"feet : " <<P.feet<<endl;
theStream<<"inches : " <<P.inches<<endl;
return theStream;
}
istream& operator >> (istream &Stream , Distance &Q)
{
Stream>>Q.feet>>Q.inches;
return Stream;
}
class Distsign : public Distance
{
char sign;
public :
Distsign():Distance()
{
sign='+' ;
}
Distsign(int f,float i,char sg):Distance(f,i)
{
sg='+' ;
sign=sg;
}
void get_dist()
{
Distance::get_dist();
char ch;
cout<<"Enter sign(+ or -) : \n" ;
cin>>ch;
if (ch=='+' )
{
sign='+' ;
}
else
{
sign='-' ;
}
}
void display() const
{
cout<<sign;
Distance::display();
}
};
int main()
{
Distsign a;
a.get_dist();
Distsign b(11,6.25,'+' );
Distsign g(100,5.5,'-' );
cout<<"\n Alpha = " ; a.display(); cout <<endl; // <--
cout<<"\n Beta = " ; b.display();cout << endl; // <--
cout<<"\n Gamma = " ; g.display(); cout << endl; // <--
return 0;
}
Enter feet : 3
Enter inches : 2
Enter sign(+ or -) :
-
Alpha = -3'2"
Beta = +11'6.25"
Gamma = +100'5.5"
Program ended with exit code: 0
Instead of the display functions returning a void, it would be better if they returned a <string> That way you can chain the cout's together the way you had originally, and maintain the easily managed format you probably had in mind.
Last edited on Nov 12, 2020 at 7:33am UTC
Nov 12, 2020 at 1:47pm UTC
You were on the right track for making
operator<< class aware but you forgot to add a friend function for your
Distsign class.
Add to the
Distsign class body:
83 84 85 86 87
friend std::ostream& operator << (std::ostream& theStream, Distsign& P)
{
theStream << P.sign << P.feet << "\' " << P.inches << '\"' ;
return theStream;
}
And change the output statements in main:
100 101 102
std::cout << "Alpha = " << a << '\n' ;
std::cout << "Beta = " << b << '\n' ;
std::cout << "Gamma = " << g << '\n' ;
All your errors were related to trying to chain an output class member function (
Distsign::display ) to the ostream. You had 3 actual errors that generated a lot of phantom errors.
Last edited on Nov 12, 2020 at 1:48pm UTC