class errror c++

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
You need to use code tags when posting code so that it retains indentation.

[code]
your code here
[/code]

Last edited on
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
94
95
96
   std::cout << "\n Alpha = "; a.display();
   std::cout << "\n Beta = "; b.display();
   std::cout << "\n Gamma = "; g.display();
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.

And PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add code tags.
Last edited on
Why not remove display() and just use << ? Consider:

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
#include <iostream>

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 << "Enter inches : ";
		cin >> inches;
	}

	friend ostream& operator << (ostream& theStream, const Distance& P);
};

ostream& operator << (ostream& theStream, const Distance& P)
{
	return theStream << P.feet << "\'"<< P.inches << '\"' << endl;
}

class Distsign : public Distance
{
	char sign;

public:
	Distsign() : Distance(), sign('+') {}
	Distsign(int f, float i, char sg) : Distance(f, i), sign(sg) {}

	void get_dist()
	{
		Distance::get_dist();
		char ch;

		cout << "Enter sign (+ or -): ";
		cin >> ch;

		sign = (ch == '+' ? '+' : '-');
	}

	friend ostream& operator<<(ostream& os, const Distsign& ds);
};

ostream& operator<<(ostream& os, const Distsign& ds)
{
	cout << ds.sign;
	return os << static_cast<Distance>(ds);
}

int main()
{
	Distsign a;
	a.get_dist();

	Distsign b(11, 6.25, '+');
	Distsign g(100, 5.5, '-');

	cout << "\nAlpha = " << a << "\nBeta = " << b << "\nGamma = " << g << '\n';
}

Default declaring Distsign's sign data member to be private is good design to prevent unwanted access outside the class.

In addition to the public and private class access specifiers you used there is another: protected.

protected prevents access from outside the class just as private does, but allows access for derived classes.
https://www.geeksforgeeks.org/public-vs-protected-in-c-with-examples/

You could change the access specifier from public to protected for your data members feet and inches in your Distance class to prevent access from outside the base and declared classes.
Topic archived. No new replies allowed.