-6

The error happen when I try to use one of my get function on parameter inside member functions. The error is:

Invalid arguments '. Candidates are : int getTotalArea() .

here is an example from my code :

        class Apartment{
    public : // ...
enum SquareType {EMPTY, WALL, NUM_SQUARE_TYPES};

        bool operator<(const Apartment& apartment); // done - need help

        int getTotalArea(); // done

        private:
        int price;
        int length;
        int width;
        SquareType** squares;

    };


    int Apartment::getTotalArea()
    {
        int count=0;
        for(int i=0;i<width;i++)
        {
            for(int j=0;j<length;j++)
            {
                if(squares[i][j]==EMPTY)
                {
                        count++;
                }
            }
        }
        return count;
    }

    bool Apartment::operator<(const Apartment& apartment)
    {
        int thisArea=this->getTotalArea();
        int paramArea=apartment.getTotalArea(); // the error line is here !!!
//the error is Invalid arguments '. Candidates are : int getTotalArea() .
        double thisRatio=((double)price)/thisArea;
        double paramRatio=((double)apartment.price)/paramArea;
        if(thisRatio==paramRatio)
        {
            return price < apartment.price;
        }
        return thisRatio<paramRatio;

    }

Have I done something wrong ?

It's the first time I'm using c++ ... by the way - any comments for the rest of the code are fine as well.

Garf365
  • 3,619
  • 5
  • 29
  • 41

2 Answers2

2

From the answer of PcAF seems you've heavily changed your initial post without modifying your question. Very bad!

However, the problem you're facing now with getTotalArea is that it isn't declared const.

See https://stackoverflow.com/a/751690/781933 for explanation.

Community
  • 1
  • 1
rfb
  • 1,107
  • 1
  • 7
  • 14
  • Still i can't understand why my code cannot go through compilation. the compiler know if I dont change anything and force me to declare it as const ? – Omer Eliyahu Jun 09 '16 at 12:59
1

Seems like you misunderstood operator overloading (as members)

When overloading some operator as member, then first operand of that operator is object on which member operator overload is called and second operand is parameter to that function (in case of binary operators).

operator + can be used as binary(2 operands) or unary operator(1 operand).

Here it seems like you want to overload binary version as member:

Apartment operator+(const Apartment& apartment1,const Apartment& apartment2);

but since first operand is object on which that member "function" is called it must take only 1 parameter (which is second operand).

Apartment operator+(const Apartment& apartment2);

Here is the second mistake:

Apartment& operator=(SquareType** squares, int length, int width, int price);

operator = is binary operator (2 operands), therefore if you want to overload it as member function it has to take exactly one parameter (which is second operand of =), not 4 parameters.

PcAF
  • 1,975
  • 12
  • 20