Error - no operator ">>" matches these operands

Pages: 12
Different timezone here I think, hence delayed response.

I tried to use this input file:
Line2D, [5, 7], [3, 8]
but the program didn't seem to be reading (or maybe displaying ) the line correctly.
In the file Line2D.cpp , your version:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ostream & operator << (ostream & out, const Line2D& l2d)
{
    out << '[' << l2d.x << ',' << "\t\t" << l2d.y << ']' << "\t\t " 
        << '[' << l2d.getX() << ',' << "\t\t" << l2d.pt2.y << ']' << "\t\t" 
        << fixed << setprecision(3) << l2d.length << '\n';
    return out;
}

istream & operator >> (istream& in, Line2D& l2d)
{
    char left = ' ', comma= ' ', right = ' ';
    in >> left >> l2d.x >> comma >> l2d.y >> right;
    
    if (!(left == '[' && comma == ',' && right == ']'))
        in.setstate(ios::failbit);
    else
        l2d.setLength();
    return in;
}


My changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ostream & operator << (ostream & out, const Line2D& l2d)
{
    out << '[' << l2d.pt1.getX() << ',' << "\t\t" << l2d.pt1.getY() << ']' << "\t\t " 
        << '[' << l2d.pt2.getX() << ',' << "\t\t" << l2d.pt2.getY() << ']' << "\t\t" 
        << fixed << setprecision(3) << l2d.length << '\n';
    return out;
}

istream & operator >> (istream& in, Line2D& l2d)
{
    char comma;
    
    if (in >> l2d.pt1 >> comma >> l2d.pt2)
        l2d.setLength();
//    else                             // not needed, fail will be set 
//        in.setstate(ios::failbit);   // if one or both pt1 or pt2 cannot be read.
    
    return in;
}


That's all I have for now. The input was definitely important - everything that follows depends on getting the correct input.
Last edited on
I tried a modified input file:
Line2D, [5, 7], [3, 8]
Line2D, [-14, 40], [50, 3]

Which was apparently being read correctly from the file, but only the first line was being stored in the set<Line2D> linetwod.

Why would that be? Well, a set checks that its elements are unique, and makes use of the comparison function
1
2
3
4
5
6
7
8
bool Line2D::operator < (const Line2D& rhs) const
{
    if (x < rhs.x)
        return true;
    if ((x == rhs.x) && (y < rhs.y))
        return true;
    return false;
}

That seems to compile, but it doesn't do anything very useful. What are x and y anyway? A line starts at pt1 and ends at pt2, each of which are points having their own x,y coordinates. Where does the extra x and y fit in to all of this?

The problem is here in Line2D.h
 
class Line2D : public Point2D

that is, a line is inheriting from Point2D, hence it gets an extra x and y.
Change that to simply
 
class Line2D


As a result, the compare function will need changing to something more suitable,
where instead of testing x and y, it will test pt1 and pt2.
You can directly re-use the same code where the only problem arises in the expression (x == rhs.x) which now becomes (pt1 == rhs.pt1). But the compiler doesn't know how to test whether two points are equal, so you need to add that in Point2.h and .cpp
1
2
3
4
bool Point2D::operator == (const Point2D & rhs) const
{
    return  ((x == rhs.x) && (y == rhs.y));
}



I feel like I've given bits of code without much explanation - you do need to try to understand it, and if it doesn't make sense, feel free to ask.

The output I get is this, which seems correct:
 P1-X           P1-Y            P2-X            P2-Y            Length
----------------------------------------------------
[-14,           40]              [50,           3]              73.926
[5,             7]               [3,            8]              2.236








Hi Chervil, after removing the public Point2D from the class Line2D. From my understanding of what you wrote above,
1
2
3
4
5
6
7
8
bool Point2D::operator < (const Point2D & rhs) const
{
    if (x < rhs.x)
        return true;
    if ((x == rhs.x) && (y < rhs.y))
        return true;
    return false;
}

i need to replace the above code with this right?
1
2
3
4
bool Point2D::operator == (const Point2D & rhs) const
{
    return  ((x == rhs.x) && (y == rhs.y));
}


However this error occur when i try to replaced it.
bool Line2D::operator==(const Line2D&) const’ member function declared in class ‘Line2D’
bool Line2D::operator == (const Line2D & rhs) const

can you guide me on how to solve it? Thanks!
i need to replace the above code with this right?

Not quite. What I wrote was a bit abbreviated.

Take the original code as a guide to the logic required
1
2
3
4
5
6
7
8
bool Line2D::operator < (const Line2D& rhs) const
{
    if (x < rhs.x)
        return true;
    if ((x == rhs.x) && (y < rhs.y))
        return true;
    return false;
}

Neither x nor y exist any more.
each place where there is x, change it to pt1
each place where there is y, change it to pt2
as follows
1
2
3
4
5
6
7
8
bool Line2D::operator < (const Line2D& rhs) const
{
    if (pt1 < rhs.pt1)
        return true;
    if ((pt1 == rhs.pt1) && (pt2 < rhs.pt2))
        return true;
    return false;
}

(Do that in the file Line2D.cpp)



The other code goes in Point2D.cpp and the declaration in Point2D.h

Note - I'm writing this post from memory, I don't have my own actual code handy to see whether I made any mistake.
Hi Chervil, how would you go about implementing the sort functions for Point2D for it's x/y coordinates?

could you guide me along?
I think I covered that in a previous post?
http://www.cplusplus.com/forum/general/201597/#msg962344

Could you explain what exactly you need help with, and show your attempt so far.
Last edited on
Hi Chervil, currently i manage to complete the Point2D part and i am left with Point3D.

Point3D is the same as Point2D. And i

Can i know how do i run the ostream code such that i can print out the int x,y z?
This is what i have currently. Thanks
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
class Point3D: public Point2D{
protected:
int z;
void setDistFrOrigin();


public:
Point3D();
Point3D(int, int, int);
int getZ() const;
void setZ(int);
void compute();
friend istream & operator >> (istream & is, Point3D & p3d);
friend ostream & operator << (ostream & os, const Point3D & p3d);
};

Point3D::Point3D(){

}

int Point3D::getZ() const
{
    return z;
}

void Point3D::setZ(int z)
{
    z = z;
}

Point3D::Point3D(int x, int y, int z)
{
 x = x;
 y = y;
 z = z;
}

void Point3D::setDistFrOrigin()
{
    distFrOrigin = sqrt((x * x) + (y * y) + (z * z));
}

void Point3D::compute()
{
    setDistFrOrigin();
}

istream & operator >> (istream & is, Point3D & p3d)
{
int x,y,z;
    char left = ' ', comma= ' ', right = ' ', last = ' ';
    is >> left >> p3d.x >> comma >> p3d.y >> right >> comma >> last >> p3d.z;
    
    if (!(left == '[' && comma == ',' && right && comma == ',' && last == ']'))
       is.setstate(ios::failbit);
	else {
	p3d.compute();
	}
    return is;
}

ostream & operator << (ostream & os, const Point3D & p3d)
{
os << '[' << p3d.x << ',' << "\t\t" << p3d.y << ',' << "\t\t" << p3d.z << ']' << "\t\t" << fixed << setprecision(3) << p3d.distFrOrigin << '\n';
    return os;
}


i can compile but it doesnt return the x y and z via the Ostream.
The input operator >> function looks a bit odd.
48
49
50
51
52
53
54
55
56
57
58
59
60
istream & operator >> (istream & is, Point3D & p3d)
{
int x,y,z;
    char left = ' ', comma= ' ', right = ' ', last = ' ';
    is >> left >> p3d.x >> comma >> p3d.y >> right >> comma >> last >> p3d.z;
    
    if (!(left == '[' && comma == ',' && right && comma == ',' && last == ']'))
       is.setstate(ios::failbit);
	else {
	p3d.compute();
	}
    return is;
}

Line 50, what are x, y, z? Suggest they are removed.

Line 51, this is ok, but for legibility I might suggest different names , such as
left, comma1, comma2, right.

Line 52 doesn't read the right data into the right variables.
'['     --> left
x-value --> p3d.x
','     --> comma
y-value --> p3d.y
','     --> right
z-value --> comma
']'     --> last
nothing --> p3d.z


Line 54 - logic error. If we write each condition on its own line it might become clearer:
1
2
3
4
5
6
7
8
9
10
11
if (!(
 (left == '[') 
 && 
 (comma == ',') 
 && 
 (right)
 && 
 (comma == ',') 
 && 
 (last == ']')
 ))


The variable right is just used as an entire condition on its own. (it will evaluate as non-zero and hence true). The same variable comma is tested twice, which is pointless.
hi, was wondering can anyone send completed codes for the program? @justastudent @mrmartin93
Last edited on
Topic archived. No new replies allowed.
Pages: 12