Error Help :) [Appreciated]

Hey there, I hope I can get pointed in the right direction on this error. Here are my 3 files

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
//fraction.h
#ifndef _FRACTION_H_
#define _FRACTION_H_

int gcd (int u, int v);

class fraction
{

private:
	int numerator;
	int denominator;

public:
	fraction(int n, int d) : numerator(n), denominator(d)
	{
		int common = gcd(numerator, denominator);

		numerator /= common;
		denominator /= common;
	}

	fraction add(fraction f);
	fraction sub(fraction f);
	fraction mult(fraction f);
	fraction div(fraction f);
	void read ();
	void print();

};
#endif 


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

fraction fraction :: add(fraction f)
{
 int n = numerator * f.denominator + denominator * f.numerator;
 int d = denominator * f.denominator;

 return fraction (n, d);

}

fraction fraction :: sub(fraction f)
{
 int n = numerator * f.denominator - denominator * f.numerator;
 int d = denominator * f.denominator;

 return fraction (n, d);
}

fraction fraction :: mult(fraction f)
{
 int n = numerator * f.numerator;
 int d = denominator * f.denominator;

 return fraction (n, d);
}

fraction fraction :: div(fraction f)
{
 int n = numerator * f.denominator;
 int d = denominator * f.denominator;

 return fraction (n, d);
}
	
	// friend fraction operator+(fraction f1, fraction f2);
	
	//fraction add(fraction f1, fraction f2) 


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
//calc.cpp
#include <iostream>
#include "fraction.h"
using namespace std;

int main ()
{
 char choice;
 fraction left;
 fraction right;
 fraction result;

 do
 {
  cout << "a\tadd\n";
  cout << "s\tsub\n";
  cout << "m\tmult\n";
  cout << "d\tdiv\n";
  cout << "e\texit\n";

  cin >> choice;
  cin.ignore();

  switch (choice)
  {
   case 'A':
   case 'a':
    cout << "Enter the first operand: ";
    left.read();
    cout << "Enter the second operand: ";
    right.read();
    result = left.add(right);
    break;

    case 'S':
   case 's':
    cout << "Enter the first operand: ";
    left.read();
    cout << "Enter the second operand: ";
    right.read();
    result = left.sub(right);
    break;

    case 'M':
   case 'm':
    cout << "Enter the first operand: ";
    left.read();
    cout << "Enter the second operand: ";
    right.read();
    result = left.mult(right);
    break;

    case 'D':
   case 'd':
    cout << "Enter the first operand: ";
    left.read();
    cout << "Enter the second operand: ";
    right.read();
    result = left.div(right);
    break;

    case 'E':
    case 'e':
     break;

    default:
     cerr << "unrecognized choice: " << choice << endl;

  }
 } while(choice != 'e' && choice != 'E');

 return 0;
}


My error is here:
1
2
3
fraction left;
 fraction right;
 fraction result;

Error: no default constructor exists for the class "fraction"

Any thoughts?

Last edited on
The problem is that when you define fraction left, right, result, you don't give any arguments. The class does not know how to initialize itself.

You need to define the default constructor. Just add this to line 22:
fraction() { }
Error: no instance of constructor "fraction::fraction" matches the argument list
is what I get when i add that to line 22

and that goes where? what file? header or calc or fraction

figured it, header
Last edited on
Which line 22 did you add it to?
now i get this:

1>------ Build started: Project: Chapter_6_Redo, Configuration: Debug Win32 ------
1>Build started 11/7/2012 11:48:33 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Chapter_6_Redo.unsuccessfulbuild".
1>ClCompile:
1> fraction.cpp
1> calc.cpp
1> Generating Code...
1>calc.obj : error LNK2019: unresolved external symbol "public: void __thiscall fraction::read(void)" (?read@fraction@@QAEXXZ) referenced in function _main
1>fraction.obj : error LNK2019: unresolved external symbol "int __cdecl gcd(int,int)" (?gcd@@YAHHH@Z) referenced in function "public: __thiscall fraction::fraction(int,int)" (??0fraction@@QAE@HH@Z)
1>c:\users\***** *******\documents\visual studio 2010\Projects\Chapter_6_Redo\Debug\Chapter_6_Redo.exe : fatal error LNK1120: 2 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.59
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I think you need to do a clean rebuild of your program.

After you get past those errors you have to address two more things:

there is no implementation for fraction::read and fraction::print.

there is no implementation for gcd anywhere in your code. that isn't in math.h is it?
Topic archived. No new replies allowed.