Why do i get a redefinition of class error

I know it is on Catalan, but its the same code as in Engrish :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>

using namespace std;

int suma();
int resta();
int divisio();
int multiplicacio();
int operacio;
int a;
int b;


int main()
{
    cout << "Selecciona el calcul a fer: \n 1. Suma \n 2. Resta \n 3. Multiplicacio \n 4. Divisio \n ";
    cin >> operacio;

    return 0;
}

int operacio{
    if (operacio==1){

        int suma;
    }

    if (operacio==2){

        int suma;
    }
    if (operacio==3){

        int suma;
    }
    if (operacio==4){

        int suma;
    }
}

int suma(){

    cout <<"Inserta un numero ";
    cin >> a;
    cout <<"Inserta un segon numero ";
    cin >> b;

    cout << a + b;

}

int resta (){

    cout <<"Inserta un numero ";
    cin >> a;
    cout <<"Inserta un segon numero ";
    cin >> b;

    cout << a - b;

}

int multiplicacio(){

    cout <<"Inserta un numero ";
    cin >> a;
    cout <<"Inserta un segon numero ";
    cin >> b;

    cout << a * b;

}

int divisio(){

    cout <<"Inserta un numero ";
    cin >> a;
    cout <<"Inserta un segon numero ";
    cin >> b;

    cout << a / b;

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int operacio{
    if (operacio==1){

        int suma;
    }

    if (operacio==2){

        int suma;
    }
    if (operacio==3){

        int suma;
    }
    if (operacio==4){

        int suma;
    }
}



i'm sorry i can't understand what you want to doing here... Because it's placed outside main, but itsn't a function; so i got confuse...
Last edited on
closed account (3qX21hU5)
I would suggest starting over at the beginning of functions and learning the basics about them. Because there is a number of things wrong here, like not returning anything from your functions when they expect a return and other things. So try rereading from the beginning or checkout our tutorial here which can be of some help and should only take 30 mins or so to read through the basic functions part.
i'm just making a calculator

first it cout what do you want to do, and then if you select 3 you select sum and you can sum 2 numbers that you add later
As Zereo has already said, you need to read up about functions; the relevant cplusplus.com tutorial articles can be found here:

http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

This bit is esp. bad from a C++ point of view.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int operacio{
    if (operacio==1){

        int suma;
    }

    if (operacio==2){

        int suma;
    }
    if (operacio==3){

        int suma;
    }
    if (operacio==4){

        int suma;
    }
}


VC++ thinks you're trying to define a function but forgot the ()

error C2470: 'operacio' : looks like a function definition,
but there is no parameter list; skipping apparent body

But it operacio was the name of a function then (a) you' be trying to set the value of a fixed function pointer to an integer value, (b) forgetting to return the int you promised you would.

And g++ thinks you're trying to redeclare an int variable called operacio.

main.cpp:23:5: error: redefinition of 'int operacio'
main.cpp:10:5: error: 'int operacio' previously declared here

As a general rule, C++ only allows operations and other functions to be used within the body of a function; similarly for control flow statements. You can not write code like that at file scope.

Andy
Last edited on
closed account (NyhkoG1T)
You are defining "operacio" as an integer VARIABLE here
 
int operacio;


Then you are using "operacio" as a function, here

1
2
3

int operacio{


so, from what i see with your program, you need to take out that whole section, which is this one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int operacio{
    if (operacio==1){

        int suma;
    }

    if (operacio==2){

        int suma;
    }
    if (operacio==3){

        int suma;
    }
    if (operacio==4){

        int suma;
    }
}


and modify your int main() { ... } function to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
    cout << "Selecciona el calcul a fer: \n 1. Suma \n 2. Resta \n 3. Multiplicacio \n 4. Divisio \n ";
    cin >> operacio;
    
    switch (operacio) {
        case 1:
            suma();
            break;

        case 2:
            resta();
            break;

        case 3:
            multiplicacio();
            break;

        case 4:
            divisio();
            break;

        default:
            cout << "You selected an inappropriate option"<<endl;


    }
    return 0;
}


Also please note that this calculator will only be able to perform operations on whole numbers. Anything with a decimal will be trunicated.

Also, you have a LOT of repetition in your functions, which the idea of functions is to save yourself from repetition. Since you have integers a and b declared in the global scope, you could you could simply do

1
2
3
4
    cout <<"Inserta un numero ";
    cin >> a;
    cout <<"Inserta un segon numero ";
    cin >> b;


one time in the main function, since your asking for the same data in each function. If you use the code that I showed you above with the switch statement, you'd simply place the code directly above, above your switch statement. However, considering the overall simplicity of these functions, it would be more optimal and organizational to have a one-does-all function. Lets take in to consideration the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using namespace std;

int operations(int opr, int integer1, int integer2) {
	switch (opr){
		case 0: // break statements not required, because we are returning from the function
			return integer1+integer2;
		case 1:
			return integer1-integer2;
		case 2:
			return integer1/integer2;
		case 3:
			return integer1*integer2;
		default:
			return integer1+integer2;
	}
}

int main() {

	int choice = 0;
	int num1 = 0;
	int num2 = 0;
	cout << "Please choose a function:"<<endl<<"  1. Add"<<endl<<"  2. Subtract"<<endl<<"  3. Divide"<<endl<<"  4. Multiply"<<endl<<endl<<"Your choice: ";
	cin >> choice;

	if((choice>0) && (choice<5)) {
		cout << endl << "Enter your first number: ";
		cin >> num1;
		cout << endl << "Enter your second number: ";
		cin >> num2;
		cout << endl << "The Result Is: " << operations(choice-1, num1, num2) << endl;
	}else{
		cout << endl <<"You chose an invalid options. Goodbye.";
	}
	return 0;
}


In this code we use one function to perform all operations we require. A function prototype is not required either because we are defining it before it's called upon. I believe the code to be pretty straigthtforward, however, if you have any questions please ask.
Last edited on
THANK YOU! IT WORKS!!

My fail was due because I'm going a bit faster than Bucky's tutorial (I'm doing things without mastered em well at all).


Thank you at all and good summer!!
Topic archived. No new replies allowed.