Invalid arguments when calling getter member - c++

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.

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.

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.

Related

A simple operator overloading program in C++ on codeblocks. Got an error at line 19. The same program runs perfectly on Turbo C++

Got an error on line 19:
error : no 'operator++(int)' declared for postfix '++' [-fpermissive]
#include<bits/stdc++.h>
using namespace std;
class sample
{
int x,y;
public:
sample(){x=y=0;}
void display()
{
cout<<endl<<"x="<<x<<" and y= "<<y;
}
void operator ++(){x++;y++;}
};
int main()
{
sample s1;
sample s2;
s1++;
++s2;
++s1;
s1.display();
s2.display();
return 0;
}
Error on code line:
s1++;
TURBO C++? Haven't heard that name in a LONG time. That is a compiler from an era before C++ was even standardized, and is not representative of C++ anymore.
Prefix increment and post-increment are different functions. You can't just overload one and expect both to work.
Also your signature is wrong. It should return a type matching the class it's called on. For example:
class sample
{
public:
...
// prefix
sample & operator ++() {x++; y++; return *this; }
// postfix (the int is a dummy just to differentiate the signature)
sample operator ++(int) {
sample copy(*this);
x++; y++;
return copy;
}
};
The value of the return type is the main difference between the two so it's pointless for your operator to return void. One returns a reference, since it represents the incremented object, while the postfix version returns a copy that is the state of the pre-incremented object.

Function overloading problems C++

I'm running an option pricing model that yields four values for four different options.
class EuroOption
{
private:
double S; //underlying stock price
double X; //strike price
double sigma; //volatility
double T; //time to expiration
double r; //risk-free rate
double b; //cost of carry
public:
EuroOption(); //default constructor
~EuroOption(); //destructor
EuroOption(const EuroOption& eo); //copy constructor
EuroOption& operator = (const EuroOption& source); //assignment operator
EuroOption(vector<double> Batch1);
EuroOption(vector<double> Batch2); //this is the error: redeclaration
//EuroOption(vector<double> const Batch3);
//EuroOption(vector<double> const Batch4);
Here is the source material from .cpp:
EuroOption::EuroOption(vector<double> Batch1) : S(60), X(65), sigma(0.30), r(0.08), T(0.25), b(r)
{
}
EuroOption::EuroOption(vector<double> Batch2) : S(100), X(100), sigma(0.20), r(0), T(1), b(r)
{
}
The errors I'm getting are "constructor cannot be redeclared". But my functions have different arguments(Batch1/Batch2) so I don't understand why it's not overloading. The output for Batch2 is also the same as Batch 1 (this is not correct). I'd be grateful for the guidance you may have.
The overloading is based on the parameter types not parameter names.
EuroOption::EuroOption(vector<double> Batch1)
Here vector<double> is the parameter type and Batch1 is the parameter name.
If you want overloading functions, you should declare functions with different parameter types or different number of parameters.
For eg, these are overloaded functions,
EuroOption::EuroOption(vector<double> Batch1)
EuroOption::EuroOption(vector<int> Batch1)
EuroOption::EuroOption(string Batch1)
I'm guessing the intent of what you want to do is to tag dispatch the constructor, or something similar, so that EuroOption is constructed with different hard-coded defaults.
struct Batch1{};
struct Batch2{};
class EuroOption
{
private:
double S; //underlying stock price
double X; //strike price
double sigma; //volatility
double T; //time to expiration
double r; //risk-free rate
double b; //cost of carry
public:
EuroOption(); //default constructor
~EuroOption(); //destructor
EuroOption(const EuroOption& eo); //copy constructor
EuroOption& operator = (const EuroOption& source); //assignment operator
EuroOption(Batch1);
EuroOption(Batch2);
.cpp file:
EuroOption::EuroOption(Batch1) : S(60), X(65), sigma(0.30), r(0.08), T(0.25), b(r)
{
}
EuroOption::EuroOption(Batch2) : S(100), X(100), sigma(0.20), r(0), T(1), b(r)
{
}
Then elsewhere in your code it could be constructed as:
EuroOption option1{Batch1{}};
EuroOption option2{Batch2{}};
These constructors are same:
EuroOption(vector<double> Batch1);
EuroOption(vector<double> Batch2);
These declarations are equivalent to:
EuroOption(vector<double>);
EuroOption(vector<double>);
It is because the function signature is the same. This is determined by the return type, name and number and type of the arguments.
Although the parameter name is different, the functions have exactly the same signature. In your example, when you call the constructor how do you decide which constructor gets called? The only difference is the parameter name but that is not accessible to the caller.
For future reference check: http://www.cplusplus.com/doc/tutorial/functions2/
Method Overloading means same method different signatures
EuroOption(vector Batch1);
EuroOption(vector Batch2);
same method has declared two times in the same class cause an error method redeclaration

Array Index operator overloaded.Unable to use the comparison operator now

I overloaded array subscript ( [] ) operator. I have made it return an integer as I wont be using it for any assignment purposes. However, I am unable to use the comparison operator now!
Here is the code
class Set
{
public:
virtual int operator[](int i) = 0;
virtual int size() = 0;
void union_operation(Set* second);
void interesction_operation(Set* second);
};
void Set::union_operation(Set* second)
{
int second_size = second->size();
for(int i=0;i<second_size;i++)
{
for(int j=0;j<this->size();j++)
{
//The line below doesnt work!
if(this[j]==second[i])
{
break;
}
}
}
}
The implementation of operator overloading is carried out in a derived class.
Since the overloaded operator will return an integer, hence the comparison is between two integers, which is perfectly valid. Why does this line still give an error?
In C++, this is a pointer that requires dereferencing before you can use it. Unless you're passing it to a function of course.
So, in order for your comparison to work, it should look like the following:
if((*this)[j] == (*second)[i])
{
break;
}
EDIT: second is also a Set pointer so you must dereference it to use it too.

Syntax for Overloading Type Conversion Operator

I'm sorry if this has been asked already, but I'm still learning C++ and struggling with a bit of syntax.
I'm supposed to overload the type conversion operator, so that it will accept an object and return an int value, based on a protected int inside that object.
Header file:
definitions.h
class Baseballs
{
protected:
int currentValue;
public:
Baseballs(int);
int operator= (Baseballs&); // ??????
}
Methods:
methods.cpp
#include "definitions.h"
Baseballs::Baseballs(int value)
{
currentValue = value;
}
int Baseballs::operator=(Baseballs &obj) // ??????
{
int temp = obj.currentValue;
return temp;
}
So in main.cpp, if I create an object:
Baseballs order(500);
Then 500 is assigned to currentValue. I need to be able to assign that to an int variable, and ultimately print it for verification, such as:
int n = order;
cout << n;
What I'm having trouble with is the syntax for overloading =. Can someone tell me what the proper syntax for the definition and method should be?
The overloaded = is really to assign to objects of the same type. Ex:
order = another_order;
What you are looking for is an overloaded conversion operator.
operator int() { return currentvalue; }
However this is generally not regarded as good practice, due to unknown conversions. An explicit overload is much safer:
explicit operator int() {...}
However you would need to do:
int n = static_cast<int>(order);

C++ conversion operators

I know the deal .The compiler tries to convert one object into the other objects's type with the help of conversion operator .Two ways to do this .Constructor (Converts a class to the other) or conversion operator .So , this one is just to test if I am thorough with the concepts .The code below gives the error
using namespace std ;
class A
{
int i ;
public:
A(int a=0){this->i=a;}
A operator+(const A& b){
A c ;
return c(this->i+b.i);
}
void show()
{
cout<<i<<endl;
}
};
int main()
{
A a1(1),a2(2),a3;
a3=a2+a1;
a3.show();
return 0;
}
I guess the error is in the operator + .When I try to assign A(i) .There is no match for an operator which could create an A from an int .
But Then I see this A's constructor lurking behind .It can convert an int into an A .Suppose , it does convert int into an A.Then , the call becomes A(B) .This is equivalent to the copy constructor .Hence , this call should work .But it doesn't .All in all , am pretty confused .
Please help .
In these two lines you are telling the compiler to construct an A object with the default constructor, then call its nonexistent operator () (int) and return its return value:
A c ;
return c(this->i+b.i);
Use either
A c(i + b.i);
return c;
or
return A(i + b.i);
On a side note, an example for an implicit conversion operator, for your class:
operator int () const
{
return i;
}
But their use smells like bad design, and can cause bad stuff to happen, like implicit conversion to bool or a pointer. Use something else instead, like an int toInt () const member function.