Help me with this POO please(ii)

I can't find the following errors, help me please:

Error 3 error C2371: 'fraction::SetData' : redefinition; different basic types line 7


Error 2 error C2556: 'fraction fraction::SetData(float,float)' : overloaded function differs only by return type from 'void fraction::SetData(float,float)' line 7


Error 1 error C2628: 'fraction' followed by 'void' is illegal (did you forget a ';'?)line 6


Error 4 error C2628: 'fraction' followed by 'void' is illegal (did you forget a ';'?)line 3


Error 5 error C3874: return type of 'main' should be 'int' instead of 'fraction'line 4


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//sumafrac.h
#include <iostream>
using namespace std;

class fraction
{
	float num,den;
public:
	void SetData(float x, float y);
	fraction(){num = den =0;}
	fraction(float x, float y);
	~fraction();
	fraction add(fraction f);
	void Prfloat(void)
	{
		cout<<num<<endl;
		cout<<"--"<<endl;
		cout<<den<<endl;
		cout<<"Se acabó";
	};
}


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
//sumafrac.cpp
#include "sumafrac.h"

void fraction::SetData(float x, float y)
{
	num = x;
	den = y;
}

fraction::fraction(float x, float y)
{
	num = x;
	den = y;
}

//Optenemos dos resultados
/*
x1   x2    ((den/y1)*x1)+((den/y2)*x2)      num     f.num
-- + -- =(-----------------------------) = ----- + ------- = A , (donde den = y1*y2)
y1   y2     (y1 * y2) = (denominador)         den     f.den
*/

fraction fraction::add(fraction f)
{
	float numerador, denominador;
	denominador = den * f.den;//(y1*y2) 
	numerador = (((denominador/den)*num)+((denominador/f.den)*f.num));//A
}

fraction::~fraction()
{
	cout << "Se ha llamado al constructor"<<endl;
}


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
//main.cpp
#include "sumafrac.h"

void main()
{
	//pedir 1ª fracción
	fraction f1;
	/*float a, b;
	cout<<"Numerador ==> ";
	cin >> a;
	cout<<"Denominador ==> ";
	cin >> b;*/
	f1.SetData(1,2); //f1.SetData(a,b);

	//pedir 2ª fracción
	/*float c,d;
	cout<<"Numerador ==> ";
	cin >> c;
	cout<<"Denominador ==> ";
	cin >> d;*/
	fraction f2(1,2);

	//hacer la suma
	f2.add(f1);

	//"imprimir por la conzola"
	f2.Prfloat();
}
Two things:

1) You forgot a semicolon at the end of your class definition:
1
2
3
4
class fraction
{
    /* ... */
}; // <-- here 


2) The return type of main should be int, not void.
Definition of class must end with a semicolon:
1
2
3
class foo
{
};

Thaks "keskiverto" and "long double main", I've chanche some thins and one of them the semicolon at the end of the class, and now it runs but my problem is that the resolt is not coherent, can you help me please?

The new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//sumafrac.h
#include <iostream>
using namespace std;

class fraction
{
	float num,den;
public:
	void SetData(float x, float y);
	fraction(){num = den =0;}
	fraction(float x, float y);
	//~fraction();
	fraction add(fraction f);
	void Print(void)
	{
		cout<<num<<endl;
		cout<<"--"<<endl;
		cout<<den<<endl;
		cout<<"THE END";
	};
};

-----------------------------------------------------------------------------------------
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
//sumafrac.cpp
#include "sumafrac.h"

void fraction::SetData(float x, float y)
{
	num = x;
	den = y;
}

fraction::fraction(float x, float y)
{
	num = x;
	den = y;
}

//Optenemos dos resultados
/*
x1   x2    ((den/y1)*x1)+((den/y2)*x2)      num     f.num
-- + -- =(-----------------------------) = ----- + ------- = A , (donde denominador = y1*y2)
y1   y2     (y1 * y2) = (denominador)       den     f.den
*/

fraction fraction::add(fraction f)
{
	fraction f3000;
	f3000.den = den * f.den;//(y1*y2) 
	f3000.num = (((f3000.den/den)*num)+((f3000.den/f.den)*f.num));//A
	return f3000;
}

/*fraction::~fraction()
{
	cout << "Se ha llamado al destructor"<<endl;
}*/


--------------------------------------------------------------------------------------

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
//main.cpp
#include "sumafrac.h"

void main()
{
	//pedir 1ª fracción
	fraction f1;
	float a, b;
	cout<<"Numerador ==> ";
	cin >> a;
	cout<<"Denominador ==> ";
	cin >> b;
	f1.SetData(a,b);

	//pedir 2ª fracción
	float c,d;
	cout<<"Numerador ==> ";
	cin >> c;
	cout<<"Denominador ==> ";
	cin >> d;
	fraction f2(c,d);

	//hacer la suma
	f2.add(f1);

	//"imprimir por la conzola"
	f2.Print();
}
I think that the mistake could be in the funcion
fraction fraction::add(fraction f)
that it is in the line 23 of the "sumafrac.cpp" and in the line 24 of the "main.cpp"
f2.add(f1);
, but I'm not totally sure, HELP ME PLEASE...
Ok. I've solve the problem was in the "main.cpp", thaks "jonanderdiez"...

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
//main.cpp
#include "sumafrac.h"

void main()
{
	//pedir 1ª fracción
	fraction f1;
	float a, b;
	cout<<"Numerador ==> ";
	cin >> a;
	cout<<"Denominador ==> ";
	cin >> b;
	f1.SetData(a,b);

	//pedir 2ª fracción
	float c,d;
	cout<<"Numerador ==> ";
	cin >> c;
	cout<<"Denominador ==> ";
	cin >> d;
	fraction f2(c,d);

	//hacer la suma
	fraction ff = f2.add(f1);

	//"imprimir por la conzola"
	ff.Print();
But, but .. you still have void main().
It should be int main()
It's defined that way in the C++ standard. If your compiler allows non-standard code, it doesn't help the learning process. Start out doing things the right way and you won't have to un-learn them in the future.
Last edited on
But I don't understand, why? I put like that allwais, can you expline me why it is int instead of void? our teacher alwais does with void
But I don't understand, why? I put like that allwais, can you expline me why it is int instead of void?

Because the standard requires it.


our teacher alwais does with void

Your teacher has a bad habit.
There is a standard to which all C++ compilers should conform. if you write int main() then it is guaranteed to be accepted by all C++ compilers.

On the other hand, if you write void main() then it may or may not be accepted, depending upon which compiler you use. When learning, it is important to start out writing standards-compliant C++ code, rather than depending upon quirks of a particular compiler.

(unfortunately those who teach do sometimes give incorrect advice ).

One of many threads on this topic:
http://stackoverflow.com/questions/4207134/what-is-the-proper-declaration-of-main
Thanks "cire" and "Chervil" really, I'm learning more in this web site that in class =).
Topic archived. No new replies allowed.