Operator overloads problems

closed account (E0MoLyTq)
Why are all my test failing for my operator overloads
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
 //Tests.cpp 
Sequence s1;
   s1.append('c');
   Sequence s2;
   s2.append('t');
   s2.append('a');
   Sequence s3;  // s3 = (c, a, t); current item c
   s3 += s1;
   s3 += s2;
   assert(s3.size() == 3);
   assert(s3.is_item());
   assert(s3.current() == 'c');
   s3.advance();
   assert(s3.current() == 'a');
   s3.advance();
   assert(s3.current() == 't');


//Operator Overloads Sequence.cpp
 bool operator==(const Sequence& seqA, const Sequence& seqB)
   {
      return true;
   }

   bool operator!=(const Sequence& seqA, const Sequence& seqB)
   {
      return false;
   }

   Sequence operator+(const Sequence& seqA, const Sequence& seqB)
   {
   }

   void Sequence::operator+=(const Sequence& otherSeq) // member, not friend
   {
   }

   std::ostream& operator<<(std::ostream& out, const Sequence& seq)
   {
      out << seq.current() << " ";
      return out;
   }

   std::istream& operator>>(std::istream& in, Sequence& seq)
   {
      in >> seq.current() >> " ";
      return in; 
   }

} // end namespace




First of all some errors I wanted to point out in your program. The first is shouldn't 'a' be appended before 't' to obtain 'cat'. And you can use only one sequence and append 'cat' to it.

Secondly I'm assuming the .current() function returns the current character and so the '==' boolean operator has two operands which are character data type. So the one you defined is not called.

Thirdly your '+=' operator is being called first at line 8 and then line 9. But your '+=' operator function is doing nothing and so the sequence s3 is empty till the end of the program. And that is why your boolean statements return false.
Last edited on
The code you've shown for operators ==, !=, + and += don't do anything. When overloading an operator you have to write the code that does whatever the overloaded operators mean.
Topic archived. No new replies allowed.