Code Blocks class File Help

Hey, I'm trying to create a class within its own file, and then functions, and what not. I'm having a bit of trouble, and know I'm doing this horribly wrong. Can someone point out some errors, and steer me in the right direction with this?

Math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MATH_H
#define MATH_H


class Math
{
    public:
       int Adding();
       int Subtracting();
       int Multiplying();
       int Dividing();
};

#endif // MATH_H 


Math.cpp

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
#include "Math.h"
#include<iostream>
using namespace std;

Math::int Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}

Math::int Subtracting(int a, int b)
{
    int difference;
    difference = a - b;
    return difference;

}

Math::int Multiplying(int a, int b)

{
    int product;
    product = a * b;
    return product;
}

Math::int Dividing(int a, int b)

{
    int quotient;
    quotient = a / b;
    return quotient;
}
Last edited on
In math.cpp your method declarations are wrong.

1
2
3
4
5
6
7
Math::int Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}


should be

1
2
3
4
5
6
7
int Math::Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}


the same goes with all your other methods.
Last edited on
Thanks :). I'm still getting errors on compile though.

Math.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MATH_H
#define MATH_H


class Math
{
    public:
       int Adding();
       int Subtracting();
       int Multiplying();
       int Dividing();
};

#endif // MATH_H 


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
#include "Math.h"
#include<iostream>
using namespace std;

Math::Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}

Math::Subtracting(int a, int b)
{
    int difference;
    difference = a - b;
    return difference;

}

Math::Multiplying(int a, int b)

{
    int product;
    product = a * b;
    return product;
}

Math::Dividing(int a, int b)

{
    int quotient;
    quotient = a / b;
    return quotient;
}


you left out the return type of the methods this time. Like I said previously.

1
2
3
4
5
6
7
Math::int Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}


should be

1
2
3
4
5
6
7
int Math::Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}



if you look closely there's a return type before the Math::Adding(int a, int b). :P
sorry, didn't catch that in the first post, i only saw one of the errors.. I'm still getting errors though..

Error - prototype for 'int Math::Dividing(int, int)' does not match any in class 'Math'

there's the same error for each function I believe. Not sure what it means.

math.cpp

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
#include "Math.h"
#include<iostream>
using namespace std;

int Math::Adding(int a, int b)
{
    int sum;
    sum = a + b;
    return sum;

}

int Math::Subtracting(int a, int b)
{
    int difference;
    difference = a - b;
    return difference;

}

int Math::Multiplying(int a, int b)

{
    int product;
    product = a * b;
    return product;
}

int Math::Dividing(int a, int b)

{
    int quotient;
    quotient = a / b;
    return quotient;
}
Oh in your math.h your method prototypes don't have the same parameters as the actual method declarations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MATH_H
#define MATH_H


class Math
{
    public:
       int Adding();
       int Subtracting();
       int Multiplying();
       int Dividing();
};

#endif // MATH_H  


should be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef MATH_H
#define MATH_H


class Math
{
    public:
       int Adding(int a, int b);
       int Subtracting(int a, int b);
       int Multiplying(int a, int b);
       int Dividing(int a, int b);
};

#endif // MATH_H  


since in the header your methods didn't have any parameters the compiler will expect the declarations to have no parameters as well. So that's where your compiler is complaining now.
Last edited on
perfect, thanks man.
okay, in the same project I'm having troubles with the main bit of code in main..

an error I'm getting is math was not declared in this scope.

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
#include<iostream>
#include<Math.h>
using namespace std;

int main(){

int ProgramOption;
int ExitNumber;
int a;
int b;

while(ExitNumber!=0){

cout << "Enter a number for what you would like to calculate \n";
cout << "1)Adding \n" << "2)Subtracting \n" << "3)Multiplying \n" << "4)Dividing \n";
cin >> ProgramOption;

switch (ProgramOption){
case 1:
cout << "Enter the first number: ";
cin >> a;
cout << endl << "Enter the second number: ";
cin >> b;

Math AddingObject;
AddingObject.Adding(a,b);
cout << endl << "the sum of the two number is: ";
break;

cout << endl << "if you would like to do another calculation /nEnter 1 to exit enter 0";
cin >> ExitNumber;

}

}


return 0;
}
try changing

#include<Math.h>

to

#include "Math.h"

See if that solves the problem. =)
sweet. That solved that problem. Now the issue is the number that gets displayed for sum..I'm not sure if the the values are getting passed to my adding function, or sum is being returned. in the class or not, but the number that gets displayed for sum is wayy off..

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
#include<iostream>
#include "Math.h"
using namespace std;

int main(){

int ProgramOption;
int ExitNumber;
int a;
int b;


while(ExitNumber!=0){

cout << "Enter a number for what you would like to calculate \n";
cout << "1)Adding \n" << "2)Subtracting \n" << "3)Multiplying \n" << "4)Dividing \n";
cin >> ProgramOption;

switch (ProgramOption){
case 1:
cout << "Enter the first number: ";
cin >> a;
cout << endl << "Enter the second number: ";
cin >> b;

Math AddingObject;
AddingObject.Adding(a,b);
int sum;
cout << endl << "the sum of the two number is: " << sum << endl;
break;

cout << endl << "if you would like to do another calculation /nEnter 1 to exit enter 0";
cin >> ExitNumber;

}

}


return 0;
}
Last edited on
line 27 and 28 is wrong. I added comments to make it clearer what's happening in the code

1
2
3
4
5
AddingObject.Adding(a,b); //adding the values but the result isn't saved anywhere
int sum; // declare a new variable which has some random value.

// prints out the value in sum, which is actually a random value
cout << endl << "the sum of the two number is: " << sum << endl;


it should be

1
2
3
4
int sum = AddingObject.Adding(a, b); // add the values and store the result in sum.

// print out the value of sum which is the result of the operation.
cout << endl << "the sum of the two number is: " << sum << endl;


I hope that clarifies things. =)
Topic archived. No new replies allowed.