How to solve several errors of this code?

Asked

Viewed 226 times

-2

I typed this program from the book that addresses classes and gave error. How to solve?

#include <iostream>

using namespace std;

class Efetua_calculo{
private:
    double total;
    double soma;
    double divide;

public:
     void mostra_calculo (void){
    cout << "Resultado das funcoes soma() e divide() e :" << endl;
    cout << soma() << endl;
    cout << divide() << endl;
    return;
    }
};
double Efetua_calculo::soma(){

int valor1 = 2;
int valor2 = 3;
total = valor1 + valor2;
return (total);

}
double Efetua_calculo::divide(){
int valor1 = 2;
int valor2 = 3;

total = (double) valor1/valor2;

return(total);
}

int main()
{
Efetua_calculo calculo;
calulo.mostra_calculo();
    return 0;
}

inserir a descrição da imagem aqui

  • 1

    What error was returned? Give us more details @Rodolfo

  • Edit your question and put the error exit there. It is easier to help you if we know what is happening.

  • take a look at the issue.

  • 8

    -1 for the bad title and for using a "photo" to present the problem, when (which makes it difficult for people with the same problem to find this question)

  • 1

    People need to stop voting robotically and analyze the problem. It’s not just typo you have there.

1 answer

3


There are typos. Maybe some very serious ones. Maybe the book example is really bad and doesn’t work at all. What then would be good to abandon book so bad. The example is very bad also to learn to program right. There are conceptual errors in it, in addition to the bad structure.

I don’t know if I solved the problem properly but it works:

#include <iostream>
using namespace std;

class Efetua_calculo {
private:
    double total; //do jeito que está este membro não deveria existir
public:
    void mostra_calculo(); //aqui só cabe a declaração e não a definição
    double soma(); //declarei essa função que não estava declarada
    double divide(); //idem, estava declarada como variável
};
double Efetua_calculo::soma(){
    int valor1 = 2;
    int valor2 = 3;
    total = valor1 + valor2;
    return (total);
}
double Efetua_calculo::divide(){
    int valor1 = 2;
    int valor2 = 3;
    total = (double) valor1/valor2;
    return(total);
}
void Efetua_calculo::mostra_calculo(){ //trouxe a implementação p/ cá
    cout << "Resultado das funcoes soma() e divide() e :" << endl;
    cout << soma() << endl;
    cout << divide() << endl;
    return;
}
int main() {
    Efetua_calculo calculo;
    calculo.mostra_calculo(); //tinha erro de digitação aqui
    return 0;
}

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

  • What would be these conceptual errors?

  • I had and threw it away, I did not learn by it not. The main one is the way it mounts the class. Creates an instance variable to store values that are only used locally in the methods. It has small problems that teach a bad coding style. Even p/something that is just being didactic (which is not). But I had so many problems, I don’t even know what the real purpose of this was. Note how different the code is. And it could change a lot more to get better.

  • learned C and C++ on its own and saw that it did not separate classes into separate files

  • 1

    For exercise purposes on something that will not be used in another context, it is not necessary to separate. In fact this way is even better this way. It only separates when it has to separate.

Browser other questions tagged

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