Я пытаюсь выполнить домашнее задание для своего урока информатики.У меня есть - по крайней мере для меня озадачивающая проблема «множественных определений» в каждом из моих отдельных определений в моем файле реализации.Мой заголовок выглядит следующим образом:
#ifndef FRACTION_H
#define FRACTION_H
using namespace std;
class fraction
{
public:
fraction (int n = 1, int d = 4); // constructor with 2 default params
~fraction(); // destructor explained more in class
fraction add(fraction); // adds two fractions returning a third
fraction subtract(fraction); // subtracts two fractions returning third
fraction multiply(fraction); // multiplies two fractions returning third
fraction divide(fraction); // divides two fractions returning a third
fraction makeFraction(double); // converts any decimal to a fraction
fraction makeImproperFraction(int, fraction); // make a mixed fraction -> improper
fraction getInverse() const; // swaps the numerator and denominator
fraction reduce(); // reduces the fraction to lowest terms
double getDecimal() const; // return fraction's decimal equivalent;
void setNumer(int); // sets the private member numerator
void setDenom(int); // sets the private member denominator
int getNumer() const; // gets the private member numerator
int getDenom() const; // gets the private member denominator
void print() const; // overloaded print the fraction to the console
void print(ostream&) const; // overloaded print the fraction to the file
private:
int numerator; // private int member for numerator
int denominator; // private int member for denominator
int gcf(int, int); // private function for greatest common factor
};
#endif // FRACTION_H
Тогда у меня есть файл реализации ...
#include <iostream>
#include <iomanip>
#include <fstream>
#include "fraction.h"
// constructor with 2 parameters
fraction::fraction(int n, int d)
{
// check for a negative denominator & adjust
if(d <= 0)
{
denominator = -d; // uses a unary operator to change the sign
numerator = -n; // " " " " " " " "
}
else if (d == 0)
{
cout << "Input error! Denominator cannot be zero!" << endl;
return;
}
else
{
denominator = d;
numerator = n;
}
}
// destructor (not used for now) ... so why define it? why mention it?
fraction::~fraction()
{
}
// reads in the appropriate value from the file and returns the value to be used as numer.
int fraction::getNumer() const
{
ifstream myInput("fractionIn.txt");
int numerator; // Declare variable to be returned
if (myInput.is_open()){ // Check to make sure file opened correctly
myInput >> numerator; // Reads in next available value into the numerator
}
else {
cout << "There was an error opening the file!" << endl << endl;
}
return numerator;
}
// ...........
int fraction::getDenom() const
{
ifstream myInput("fractionIn.txt");
int denominator; // Declare variable to be returned
if (myInput.is_open()){ // Check to make sure file opened correctly
myInput >> denominator; // Reads in next available value into the denominator
}
else {
cout << "There was an error opening the file!" << endl << endl;
}
return denominator;
}
// Find the sum of the fraction with the parameter fraction.
// Returns the sum of the two fractions
fraction fraction::add(fraction a)
{
int num1 = numerator * a.denominator; // local numer1 to this fradtion
int num2 = a.numerator * denominator; // local numer2 to this fraction
int denom = denominator * a.denominator; // local common denominator to both
fraction result(num1 + num2, denom); // local fraction object
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult; // " " " " "
return result; // return the local fraction
}
// Find the difference of the fraction with the parameter fraction.
// Returns the difference of the two fractions
fraction fraction::subtract(fraction a)
{
int num1 = numerator * a.denominator; // local numer1 to this fradtion
int num2 = a.numerator * denominator; // local numer2 to this fraction
int denom = denominator * a.denominator; // local common denominator to both
fraction result(num1 - num2, denom); // local fraction object
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult; // " " " " "
return result; // return the local fraction
}
// Find the product of the fraction with the parameter fraction.
// Returns the product of the two fractions
fraction fraction::multiply(fraction a)
{
int numer = numerator * a.numerator; // local common numerator to both fractions
int denom = a.denominator * denominator; // local common denominator to both
fraction result(numer, denom); // local fraction object
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult; // " " " " "
return result; // return the local fraction
}
// Find the quotient of the fraction with the parameter fraction.
// Returns the quotient of the two fractions
fraction fraction::divide(fraction a)
{
int numer = numerator * a.denominator; // local common numerator to both fractions
int denom = a.numerator * denominator; // local common denominator to both
fraction result(numer, denom); // local fraction object
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult; // " " " " "
return result; // return the local fraction
}
// gcf returns the greatest common factor
// used to reduce fractions
int fraction::gcf(int n, int d) // uses Euler's gcf algorithm
{
int resGCF; // declaration of result GCF
int remainder = 0; // initialize remainder to zero
while (d != 0) // loops until denominator == 0
{
remainder = n % d; // remainder of the n/d division
n = d; // assign denominator to numerator
d = remainder; // assign remainder to denominator
} // end of while loop
resGCF = n; // assign numerator to result GCF
return resGCF; // return result GCF
}
//make a mixed fraction -> improper
//Returns an improper fraction
fraction fraction::makeImproperFraction(int a, fraction b) {
a *= b.denominator; //Multiply whole number by denominator to set it to proper value
b.numerator += a; //Add new value for modified whole number to numerator for improper fraction
fraction result (b.numerator, b.denominator); //store new numerator over original denominator into the result
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult; // " " " " "
return result; //Return this result
}
fraction fraction::reduce() {
fraction result (numerator, denominator);
int gcfResult = fraction::gcf(result.numerator, result.numerator); //Find gcf for result
result.numerator = result.numerator / gcfResult; //Divide both sides by gcf
result.denominator = result.denominator / gcfResult;
return result;
}
Проблема заключается в том, что Code :: Blocks постоянно сообщает мне, что существует несколько определений"для каждого из моих определений функций-членов, хотя, насколько я могу судить, есть только одна часть ... Журнал ошибок вставляется ниже
||=== Build: Debug in lab1redux (compiler: GNU GCC Compiler) ===|
obj\Debug\fractionTest.o||In function `ZN8fractionC2Eii':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|14|multiple definition of `fraction::fraction(int, int)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|14|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fractionC2Eii':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|14|multiple definition of `fraction::fraction(int, int)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|14|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fractionD2Ev':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|35|multiple definition of `fraction::~fraction()'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|35|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fractionD2Ev':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|35|multiple definition of `fraction::~fraction()'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|35|first defined here|
obj\Debug\fractionTest.o||In function `ZNK8fraction8getNumerEv':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|41|multiple definition of `fraction::getNumer() const'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|41|first defined here|
obj\Debug\fractionTest.o||In function `ZNK8fraction8getDenomEv':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|55|multiple definition of `fraction::getDenom() const'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|55|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction3addES_':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|70|multiple definition of `fraction::add(fraction)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|70|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction8subtractES_':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|84|multiple definition of `fraction::subtract(fraction)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|84|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction8multiplyES_':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|98|multiple definition of `fraction::multiply(fraction)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|98|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction6divideES_':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|111|multiple definition of `fraction::divide(fraction)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|111|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction3gcfEii':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|124|multiple definition of `fraction::gcf(int, int)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|124|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction20makeImproperFractionEiS_':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|140|multiple definition of `fraction::makeImproperFraction(int, fraction)'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|140|first defined here|
obj\Debug\fractionTest.o||In function `ZN8fraction6reduceEv':|
C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|149|multiple definition of `fraction::reduce()'|
obj\Debug\fraction.o:C:\Users\user\OneDrive - VCCS Student Advantage\School Work\CSC210\Lab1 Redux\lab1redux\fraction.cpp|149|first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 27 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|
Что здесь происходит?Я не могу на всю жизнь положить палец на то, что вызывает эту проблему.Я проверил, чтобы убедиться, что я не определил это несколько раз.Я сократил свой main.cpp до не более чем возврата, чтобы попытаться свести к минимуму проблему, удалить участников и т. Д., И я в растерянности.
Также я понимаю, что есть некоторые «неутвержденные»тактика, используемая в заголовке, такая как «использование пространства имен std;»это вне моего контроля;Профессор хочет, чтобы мы использовали заголовочный файл как есть, чтобы выполнить задачу, не изменяя ни одного из членов или содержимого в ней.