class type comparison operator overloading

I'm trying to overload the comparison operator for a stock class I have created, but whenever I try to compile it gives me an error that says "request for members of non-aggregate type before '(' token" and "passing `const stock' as `this' argument of `int stock::getDate()' discards qualifiers"

What am I doing wrong?

stock.h
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
#ifndef STOCK_H
#define STOCK_H

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;

class stock
{    
      private: 
         int date;
         string ticker;
         double openPrice;
         double highPrice;
         double lowPrice;
         double closePrice;
         unsigned int traded;
         double changePercent;

      public: 
         // constructor
         stock(int, string, double, double, double, double, unsigned int);
         
         // get functions
         int getDate();
         string getTicker();
         double getOpen();
         double getHigh();
         double getLow();
         double getClose();
         unsigned int getTraded();
         double getChange();
         
         // operator overloading
         bool operator==(const stock &s2);         
         
         bool operator<(const stock &other);
         
};
#endif 


relevant part of stock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bool stock::operator==(const stock &other)
{
   if(*this.getDate() == &other.getDate() && *this.getTicker() == &other.getTicker()) 
      return true;

   return false;
}
     
bool stock::operator<(const stock &other)
{
   // check if the date is less 
   if(*this.getDate() < &other.getDate())
      return true;
   // if the date is the same check the ticker   
   else if(*this.getDate() == &other.getDate())
   if(*this.getTicker() < &other.getTicker()) 
      return true;                  

   return false;
}
Last edited on
Maybe not the solution but I don't think you can use the this keyword like that.

You can have:
 
(*this).getDate()

or, better yet:
 
this->getDate()


But I don't think what's in your current code is valid.
Last edited on
First, you don't need to explicitly call this at all, if you don't want to, you can just call getDate() and it implicitly uses this.

Second, the problem is that in your operators your parameters are const stock objects, which means that only const member functions can be used on those objects, notice your getDate() member is not a const member function (but it should be), in fact I would make all accessors that don't change the members of the object const, including your operators (== and <).

e.g.
1
2
3
4
bool stock::operator==(const stock &other)const
{
   return ((getDate() == other.getDate()) && (getTicker() == other.getTicker())) ;
}
Last edited on
thanks for your help. I finally got it to work by using date == other.date etc... It seems weird that I had to do it that way, but the accesser functions wouldn't compile.
Topic archived. No new replies allowed.