- #1
John O' Meara
- 330
- 0
I'm trying to write a program in C++ that reads, writes, adds and substracts numbers entered as C-style strings. I have two questions. Is it necessary that the parameters of the + operator only be always objects of a class? Can an operator call another operator from within it, e.g.
my_float operator - (my_float &op1, my_float &op2)
{
my_float temp;
if (op2.num[0] == '+')
op2.num[0] = '-';
temp = operator-(op1, op2);
return temp;
}
The above piece of code causes a segmentation fault, why? Are the arguments in the code of the operator - wrong? I do not know what else to place there. Thanks very much.
my_float operator - (my_float &op1, my_float &op2)
{
my_float temp;
if (op2.num[0] == '+')
op2.num[0] = '-';
temp = operator-(op1, op2);
return temp;
}
The above piece of code causes a segmentation fault, why? Are the arguments in the code of the operator - wrong? I do not know what else to place there. Thanks very much.