How to set a pointer as the default parameter in C++?

Asked

Viewed 113 times

3

I am "programming in C", but compiling using the extension .cpp and the g++ to have some facilities. My goal is to make a program that receives a starting salary and calculates the final salary according to certain bonus. The bonus is given in percentage over the initial salary, and if the function is called without being passed a value for the bonus, it must assume the value of 15%.

What I did:

#include <cstdio>

float calculaSalario(float* salario, float* bonificacao = 15.0f);

int main (void)
{
    float salario = 0;
    float bonificacao = 0;

    scanf("%f", &salario);
    scanf("%f", &bonificacao);

    calculaSalario(&salario, &bonificacao);

    printf("%f\n", salario);

    return 0;
}

float calculaSalario(float* salario, float* bonificacao = 15.0f)
{
    *salario = *salario + *salario * (*bonificacao/100);
}

However, I am getting the following errors:

  • error: could not float calculaSalario(float* salario, float* bonus = 15.0f);

  • In function ‘float calculaSalario(float*, float*)’: error: default argument given for parameter 2 of ‘float calculaSalario(float*, float*)’ [-fpermissive] note: previous specification in ‘float calculaSalario(float*, float*)’ here float calculaSalario(float* salario, float* bonus = 15.0f);

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

5

The first obvious problem is use float for monetary value, may not. But by a tremendous misfortune does not generate error, only causes problems sometimes, and the programmer thinks it is right.

Another error is using pointer in this function, it makes no sense, mainly because it already predicts returning something. In fact what the function returns is not the salary, so the correct is not to keep in the same variable, works and is right, but it is more readable and organized to keep in a variable that is the salary bonus. It’s not cool to repurpose variable to something else.

#include <cstdio>

float calculaSalario(float salario, float bonificacao = 15.0) { return salario + salario * (bonificacao / 100); }

int main() {
    float salario = 0;
    float bonificacao = 0;
    scanf("%f", &salario);
    scanf("%f", &bonificacao);
    salario = calculaSalario(salario, bonificacao);
    printf("%f\n", salario);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Thank you very much for your answer, it really solves the problem. But, if I wanted to use the pointers, even 'not making sense', how should I proceed? I can set a pointer as the default parameter?

  • What does that part mean? -> float calculatorSalario(float salary, float bonus = 15.0) bonus gets 15.0 before being used in the function ?

  • 1

    @Rogi93, the function can be called without passing any value to "bonus", and if it is called so, the variable bonus will assume as a standard the value "15.0".

  • 1

    @Miloski I can’t imagine why do something that doesn’t make sense. It is the same as saying "I need the result of a number divided by 2, but I will divide by 3". But if you do not use the value default, because the guy turned out to be another, so as the error indicates it makes no sense to have a value float as default from it, it would have to be a memory address, which would make even less sense. When you make a mistake, others see it together. Programming based on assumptions does not work. Nor did I mention that since you are using "facilities" of C++ nor should you use the pointer where you can.

  • 1

    @Rogi93 yes, but if you need more details ask a new question on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.