我正在尝试为我的计算机科学课完成一个家庭作业项目。我有一个 - 至少让我在我的实现文件中的每一个单一定义中对“多个定义”的问题感到困惑。我的标题如下:
#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减少到只是为了尝试最小化问题,删除成员等等,我不知所措。
我也理解在标题中使用了一些“未经批准”的策略,例如“using namespace std;”那是我无法控制的;教授希望我们按原样使用头文件来完成任务,而不是修改其中的任何成员或内容。
答案 0 :(得分:0)
问题 - 您正在包含来自另一个cpp文件的cpp文件。这是正确的选择是非常不寻常的。
您的错误告诉您源文件fraction.cpp
已编译为定义多个函数。 您的源文件fractionTest.cpp
第二次定义相同的功能。因为它包含fraction.cpp
的全部内容。
删除此行。
#include "fraction.cpp"
如有必要,请将此替换为以下行:
#include "fraction.h"
您不希望两个cpp文件定义相同的功能。这违反了One Definition Rule。