-
A tragedy!
I'm getting this infamous error when i try to compile this...
fraction\fraction.h(18) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
client.cpp
fraction\fraction.h(17) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
Code:
#ifndef FRACTION_H
#define FRACTION_H
#include <iostream>
using namespace std;
class Fraction {
public:
Fraction();
Fraction(int);
Fraction(int, int);
double getFraction() const {return ((double) numerator / denominator);}
int getNumerator() const {return numerator;}
int getDenominator() const {return denominator;}
friend const Fraction operator+ (const Fraction& x, const Fraction& y);
friend const Fraction operator- (const Fraction& x, const Fraction& y);
friend const Fraction operator* (const Fraction& x, const Fraction& y);
friend const Fraction operator/ (const Fraction& x, const Fraction& y);
friend bool operator == (const Fraction& x, const Fraction& y);
friend const Fraction operator- (const Fraction&);
friend ostream& operator<< (ostream& x, const Fraction& y);
friend istream& operator>> (istream& x, Fraction& y);
private:
void reduce();
int gcd(int, int);
int numerator, denominator;
};
#endif
----------------------------
#include <iostream>
#include <cmath>
#include <string>
#include "Fraction.h"
using namespace std;
Fraction::Fraction() : numerator(0), denominator(1) { }
Fraction::Fraction(int n) : numerator(n), denominator(1) { }
Fraction::Fraction(int n, int d) {
if (0 == d) {
cout << "Illegal value for denominator" << endl;
exit(1);
}
numerator = n;
denominator = d;
}
const Fraction operator+ (const Fraction& f1, const Fraction& f2) {
int d3 = f1.denominator * f2.denominator;
int n1 = f2.denominator * f1.numerator;
int n2 = f1.denominator * f2.numerator;
int n3 = n1 + n2;
Fraction f(n3, d3);
f.reduce();
return f;
}
const Fraction operator- (const Fraction& f1, const Fraction& f2) {
int d3 = f1.denominator * f2.denominator;
int n1 = f2.denominator * f1.numerator;
int n2 = f1.denominator * f2.numerator;
int n3 = n1 - n2;
Fraction f(n3, d3);
f.reduce();
return f;
}
const Fraction operator* (const Fraction& f1, const Fraction& f2) {
int n3 = f1.numerator * f2.numerator;
int d3 = f1.denominator * f2.denominator;
Fraction f(n3, d3);
f.reduce();
return f;
}
const Fraction operator/ (const Fraction& f1, const Fraction& f2) {
int n3 = f1.numerator * f2.denominator;
int d3 = f1.denominator * f2.numerator;
Fraction f(n3, d3);
f.reduce();
return f;
}
bool operator== (const Fraction& f1, const Fraction& f2) {
int a = f1.numerator * f2.denominator;
int b = f1.denominator * f2.numerator;
return a == b;
}
const Fraction operator- (const Fraction& f1) {
int n = -f1.numerator;
int d = f1.denominator;
if ((n < 0) && (d < 0)) {
-n;
-d;
}
else if (d < 0) {
-n;
-d;
}
Fraction f(n, d);
return f;
}
void Fraction::reduce() {
int g = gcd(abs(numerator), abs(denominator));
numerator /= g;
denominator /= g;
}
ostream& operator<< (ostream& output, const Fraction& f1) {
if (((f1.numerator < 0) || (f1.denominator < 0))
&& !((f1.numerator < 0) && (f1.denominator < 0)))
output << " - ";
output << abs(f1.numerator) << " / " << abs(f1.denominator);
return output;
}
istream& operator>> (istream& input, Fraction& f1) {
int n, d;
char ch;
input >> n >> ch >> d;
if (ch != '/') {
cout << "Fraction format error." << endl;
exit(1);
}
if (0 == d) {
cout << "Illegal value for denominator." << endl;
exit(1);
}
f1.numerator = n;
f1.denominator = d;
return input;
}
int Fraction::gcd(int x, int y) {
if (x % y == 0)
return y;
return gcd(y, x % y);
}
--------------------
#include <iostream>
using namespace std;
#include "fraction.h"
int main()
{
fraction first(1, 2);
fraction second (2,4);
fraction result=first+second;
return 0;
}
-
It compiles for me, (well, it does after I corrected the error, (fraction -> Fraction). MS VC++ 6.0 Pro.
-
If it still doesn't work, you can try downloading the service pack for MSVC, if you haven't already.. What version are you using?
I remember a similar (or perhaps the same) error happening to me over a year ago that was only corrected by installing the service pack (I still use MSVC v5.0)
-
I had that error a couple of weeks ago. I think I just rebooted and everything was hunky dorey again. Certainly didn't cause too many hassles.
-
...
O.K.
I changed the program around and it gave me a weird number...
Code:
#ifndef _FRACTION_H
#define _FRACTION_H
#include <iostream>
using namespace std;
class Fract
{
friend Fract & operator + (Fract first, Fract second);
friend Fract & operator - (Fract first, Fract second);
friend Fract & operator * (Fract first, Fract second);
friend Fract & operator / (Fract first, Fract second);
friend ostream & operator << (ostream & output, const Fract & f);
public:
Fract (int numer, int denom);
private:
int numer;
int denom;
int gcf(int a, int b);
Fract reduce(Fract x);
};
#endif
----------------------------
#include <iostream>
#include "fraction.h"
using namespace std;
ostream & operator << (ostream & output, const Fract & f)
{
output<< f.numer<< "/"<< f.denom<< endl;
return output;
}
Fract & operator+(Fract first, Fract second)
{
if(first.denom !=second.denom)
{
first.numer=first.numer*second.denom;
second.numer=second.numer*first.denom;
first.denom *= second.denom;
}
int sumNumer= first.numer + second.numer;
int newDenom = first.denom;
Fract Result(sumNumer, newDenom);
return Result;
}
Fract & operator-(Fract first, Fract second)
{
if(first.denom !=second.denom)
{
first.numer=first.numer*second.denom;
second.numer=second.numer*first.denom;
first.denom*= second.denom;
}
int subNumer= first.numer-second.numer;
int subDenom = first.denom;
Fract Result(subNumer, subDenom);
return Result;
}
Fract & operator *(Fract first, Fract second)
{
int mulNumer=first.numer*second.numer;
int mulDenom=second.denom*first.denom;
Fract Result (mulNumer, mulDenom);
return Result;
}
Fract & operator /(Fract first, Fract second)
{
int divNumer=first.numer*second.denom;
int divDenom=first.denom*second.numer;
Fract Result(divNumer, divDenom);
return Result;
}
Fract:: Fract(int x, int y)
:numer(x), denom(y)
{}
int Fract::gcf(int a, int b)
{
{
int num1, num2, reminder;
if (a >= b)
{
num1 = a;
}
else
{
num1 = b;
}
if (a <= b)
{
num2 = a;
}
else
{
num2 = b;
}
if (num1 == 0 || num2 == 0)
{
return 0;
}
while ( (reminder = (num1 % num2)) != 0)
{
num1 = num2;
num2 = reminder;
}
return num2;
};
}
Fract Fract::reduce(Fract f)
{
int divisor=0;
while ( (divisor = gcf(f.numer, f.denom)) > 1)
{
f.numer /= divisor;
f.denom /= divisor;
}
return f;
}
--------------------------------------
#include "fraction.h"
#include <iostream>
using namespace std;
int main()
{
Fract first(1, 2);
Fract second(2, 4);
cout<< first+second<< endl;
return 0;
}