mit dem folgenden Programm Ich bekomme eine Menge Kompilierungsfehler, wenn ich es versuche und kompiliere es. Ich denke, sie haben etwas zu tun mit Methode aufrufen. Ich bin nicht vertraut mit C++ und weiß nicht, was sie verursacht. Kann jemand helfen? Hier ist der Quellcode:Methoden aufrufen C++, Neuling ... Fehler kompilieren BITTE HELFEN
#include <iostream>
using namespace std;
//getting the greatest common divisor to help reduce fractions
int GDC(int x, int y)
{
int t;
while (y!=0)
{
t=y;
y=x%y;
x=t;
}
return x;
}
//constructing a RationalNumber wit a numerator and a denominator as seperate elements.
//it also reduces the fraction as well as show them.
//prevents 0 in the denominator as well as avoid negative denominators
class RationalNumber
{
public:
RationalNumber(int num=0, int denom=0)
{
numerator=num;
denominator=denom;
}
void show()
{
{
cout << "(" << numerator <<"/";
cout << denominator << ")";
}
}
void reduce()
{
int reduction;
reduction= GCD(numerator, denominator);
numerator=numerator/reduction;
denominator=denominator/reduction;
if(denominator == 0)
{
cout << "Cannot compute with 0 denominator ";
}
if(denominator < 0)
{
cout << "Cannot compute negative demoninators ";
}
else
{
//simplifies fraction to a whole number when denominator is 1
if(denominator == 1)
{
cout << "(" << numerator <<")";
}
else
{
cout << "(" << numerator <<"/";
cout << denominator << ")";
}
}
}
RationalNumber operator+(RationalNumber op2);
RationalNumber operator-(RationalNumber op2);
RationalNumber operator*(RationalNumber op2);
RationalNumber operator/(RationalNumber op2);
RationalNumber operator++();
private:
double numerator, denominator;
};
void Accept(int n)
{
if(n<0)
{
cout << "Cannot have negatice denominators: ";
}
else if (n == 0)
cout << "Cannot have zero in the denominator: ";
}
//Division
RationalNumber RationalNumer::operator/(RationalNumber op2)
{
RationalNumber temp;
int reduction;
temp.numerator=(numerator*op2.denominator);
temp.denominator=denominator*op2.numerator;
cout << "Division: ";
return temp;
}
//Subtraction
RationalNumber RationalNumer::operator-(RationalNumber op2)
{
RationalNumber temp;
int reduction;
temp.numerator=(numerator*op2.denominator)-(op2.numerator*denominator);
temp.denominator=op2.denominator* denominator;
cout << "Subtraction: ";
return temp;
}
//Multiplication
RationalNumber RationalNumer::operator*(RationalNumber op2)
{
RationalNumber temp;
temp.numerator=(op2.numerator*numerator);
temp.denominator=op2.denominator* denominator;
cout << "Multiplication: ";
return temp;
}
//Addition
RationalNumber RationalNumer::operator+(RationalNumber op2)
{
RationalNumber temp;
temp.numerator=(op2.numerator*denominator) + ((numerator)*(op2.denominator));
temp.denominator=op2.denominator* denominator;
cout << "Addition: ";
return temp;
}
RationalNumber RationalNumer::operator++()
{
numerator++;
denominator++;
return *this;
}
void getFraction(int &x, int &y, int &z, int &t)
{
cout << "First Fraction: ";
cout << "\n";
cout << "numerator: ";
cin >> x;
do
{
cout << "denominator: ";
cin >> y;
Accept(y);
cout << "\n";
cout << endl;
}
while(y <=0);
cout << "Second Fraction: ";
cout << "\n";
cout << "numerator: ";
cin >> z;
do
{
cout << "denominator: ";
cin >> t;
Accept(t);
cout << "\n";
cout << endl;
}
while(t <=0);
}
void aftermath(int &x, int &y, int &z, int &t)
{
RationalNumber obA, obS, obM, obD, ob2(x,y), ob3(z,t);
char Math;
cout << "Enter sign (+,-,*,/): ";
cin >> Math;
if(Math == '+')
{
obA=ob2+ob3;
obA.reduce();
cout << "\n";
}
else if(Math == '-')
{
obS=ob2-ob3;
obS.reduce();
cout << "\n\n";
}
else if(Math == '*')
{
obM=ob2*ob3;
obM.reduce();
cout << "\n\n";
}
else if(Math == '/')
{
obD=ob2/ob3;
obD.reduce();
cout << "\n\n";
}
else
cout << "Invalid sign";
}
int main()
{
char answer;
do
{
int x,y,z,t = 0;
getFraction(x,y,z,t);
RationalNumber obA, obS, obM, obD, ob2(x,y), ob3(z,t);
ob2.show();
cout <<" ";
ob3.show();
cout << "\n\n";
aftermath(x,y,z,t);
cout << "\n\n";
cout << "Create another fraction? - y or n? ";
cin >> answer;
cout << "\n";
}
while((answer == 'y'));
if((answer != 'y'))
{
cout << "GOOD-BYE ";
}
return 0;
}
und hier werden die kompilieren Fehler:
g++ -o Math math.cpp
math.cpp: In member function 'void RationalNumber::reduce()':
math.cpp:37: error: 'GCD' was not declared in this scope
math.cpp: In function 'void getFraction(int&, int&, int&, int&)':
math.cpp:136: error: invalid operands of types '<unresolved overloaded function type>' and 'const char [2]' to binary 'operator<<'
math.cpp:151: error: invalid operands of types '<unresolved overloaded function type>' and 'const char [2]' to binary 'operator<<'
Vielen Dank Jungs
Neuer Fehler in der aktualisierten Ausgabe - ein weiterer Tippfehler: Anzahl << "\ n"; sollte cout << "\ n" sein ;. – RichieHindle
@Richie behoben Tippfehler – marcog