包含适当的标头时,实现文件中的多个定义出错

Error with Multiple Definitions in implementation file when including appropriate header

本文关键字:定义 出错 文件 包含适 实现      更新时间:2023-10-16

我正在尝试为我的计算机科学课完成一个家庭作业项目。 我有一个 - 至少对我来说,在我的实现文件中的每一个单一定义中都有"多个定义"的令人困惑的问题。我的标题如下:

#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) ===|
objDebugfractionTest.o||In function `ZN8fractionC2Eii':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|14|multiple definition of `fraction::fraction(int, int)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|14|first defined here|
objDebugfractionTest.o||In function `ZN8fractionC2Eii':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|14|multiple definition of `fraction::fraction(int, int)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|14|first defined here|
objDebugfractionTest.o||In function `ZN8fractionD2Ev':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|35|multiple definition of `fraction::~fraction()'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|35|first defined here|
objDebugfractionTest.o||In function `ZN8fractionD2Ev':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|35|multiple definition of `fraction::~fraction()'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|35|first defined here|
objDebugfractionTest.o||In function `ZNK8fraction8getNumerEv':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|41|multiple definition of `fraction::getNumer() const'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|41|first defined here|
objDebugfractionTest.o||In function `ZNK8fraction8getDenomEv':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|55|multiple definition of `fraction::getDenom() const'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|55|first defined here|
objDebugfractionTest.o||In function `ZN8fraction3addES_':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|70|multiple definition of `fraction::add(fraction)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|70|first defined here|
objDebugfractionTest.o||In function `ZN8fraction8subtractES_':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|84|multiple definition of `fraction::subtract(fraction)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|84|first defined here|
objDebugfractionTest.o||In function `ZN8fraction8multiplyES_':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|98|multiple definition of `fraction::multiply(fraction)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|98|first defined here|
objDebugfractionTest.o||In function `ZN8fraction6divideES_':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|111|multiple definition of `fraction::divide(fraction)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|111|first defined here|
objDebugfractionTest.o||In function `ZN8fraction3gcfEii':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|124|multiple definition of `fraction::gcf(int, int)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|124|first defined here|
objDebugfractionTest.o||In function `ZN8fraction20makeImproperFractionEiS_':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|140|multiple definition of `fraction::makeImproperFraction(int, fraction)'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|140|first defined here|
objDebugfractionTest.o||In function `ZN8fraction6reduceEv':|
C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.cpp|149|multiple definition of `fraction::reduce()'|
objDebugfraction.o:C:UsersuserOneDrive - VCCS Student AdvantageSchool WorkCSC210Lab1 Reduxlab1reduxfraction.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)) ===|

这是怎么回事? 我一辈子都无法指出导致此问题的原因。 我已经检查以确保我没有多次定义它。 我已经减少了我的主要.cpp只不过是返回以尝试最小化问题、删除成员等,我不知所措。

我也知道在标题中使用了一些"未经批准"的策略,例如"使用命名空间 std";这是我无法控制的;教授希望我们按原样使用头文件来完成任务,而不是修改其中的任何成员或内容。

问题 - 您正在包含来自另一个 cpp 文件的 cpp 文件。 这是非常不寻常的正确选择。

您的错误告诉您源文件fraction.cpp已编译为定义多个函数。您的源文件fractionTest.cpp再次定义相同的函数。 因为它包括fraction.cpp的全部内容。

删除此行。

#include "fraction.cpp"

如有必要,请将其替换为以下行:

#include "fraction.h"

您不希望两个 cpp 文件定义相同的函数。 这违反了"一个定义规则"。