### Error Help :) [Appreciated]

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

 ``12345678910111213141516171819202122232425262728293031`` ``````//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 ``````

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````//fraction.cpp #include #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) ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````//calc.cpp #include #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:
 ``123`` ``````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

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.