尝试重载运算符时为什么会出现Link错误?

问题描述

我正在做一个项目练习,在这里我要修改我创建的名为polynomial的类的先前项目,以使用链接列表(原始使用的数组)。链接列表使用模板,以便可以将任何类型传递给模板。

我在此项目练习中遇到的一个问题是,我试图传递polyNumber类型的对象(来自我制作的类),并且我制作的链接列表包具有一个可比较的函数传递给它的任何项(使用==)。

它可以与常规类型(例如intstring)配合使用,但是会遇到自定义对象类型的问题。因此,我弄清楚了如何在==类中重载polyNumber运算符。就其本身而言,当我测试此类时,它可以工作,但是当我将polynomial类与链接列表实现一起使用此类型时,对于多项式类中的每个方法,都会收到诸如以下错误

Error   LNK2005 "public: __thiscall polyNumber::polyNumber(int,int)" (??0polyNumber@@QAE@HH@Z) already defined in polynomial.obj    Project11

这是这些文件代码,但是正如您在代码中看到的那样,此代码还包含其他文件,例如,链接列表对象的LinkedBag,但是对于空间,我只包含了这些文件

polyNumber.h

#pragma once

class polyNumber
{
public:
    polyNumber();
    polyNumber(int set_coefficent,int set_degree);
    void setDegree(int set);
    void setCoefficient(int set);
    int getDegree();
    int getCoefficient();
    friend bool operator== (const polyNumber& p1,const polyNumber& p2);
    friend bool operator!= (const polyNumber& p1,const polyNumber& p2);

private:
    int degree;
    int coefficient;
};

polyNumber.cpp

#include "polyNumber.h"


polyNumber::polyNumber()
{
    coefficient = 0;
    degree = 0;
}
polyNumber::polyNumber(int set_coefficent,int set_degree)
{
    coefficient = set_coefficent;
    degree = set_degree;
}

void polyNumber::setDegree(int set)
{
    degree = set;
}
void polyNumber::setCoefficient(int set)
{
    coefficient = set;
}
inline int polyNumber::getDegree()
{
    return degree;
}
inline int polyNumber::getCoefficient()
{
    return coefficient;
}

bool  operator== (const polyNumber& p1,const polyNumber& p2)
{
    return (p1.coefficient == p2.coefficient && p1.degree == p2.degree);
}

bool operator!= (const polyNumber& p1,const polyNumber& p2)
{
    return !(p1 == p2);
}

polynomial.h

#pragma once
#include "polynomialInterface.h"
#include "LinkedBag.cpp"
#include "polyNumber.cpp"
static const int polyNOMIAL_SIZE = 10;

class polynomial : public polynomoal_Interface
{
public:
    //Cunstructs am empty polynomial  
    polynomial();

    //copy constructor
    polynomial(polynomial& copy);

    /** Cunstructs a polynomial with a client defined polynomial
    @param an array of non-negative integer coeffient that does not exceed polyNOMIAL_SIZE,each coeffient in the array has a power that correspounds
    to the respective value of the location of the ceffient in that array. */
    polynomial(int coeffient[polyNOMIAL_SIZE],int size);

    int degree();
    int coefficient(int power);
    bool changeCoefficient(int newCoefficient,int power);
private:
    //static const int polyNOMIAL_SIZE = 10;
    //int polynomial[polyNOMIAL_SIZE];
    LinkedBag<polyNumber> bag;
};

polynomial.cpp

#include "polynomial.h"


polynomial::polynomial()
{

}
polynomial::polynomial(polynomial& copy)
{
    std::vector<polyNumber> copyFrom = copy.bag.toVector();
    for (int i = 0; i < copyFrom.size(); i++)
    {
        bag.add(copyFrom[i]);
    }
}
polynomial::polynomial(int coeffient[polyNOMIAL_SIZE],int size)
{
    for (int i = 0; i <= size; i++)
    {
        polyNumber number = { coeffient[i],i + 1 };
        bag.add(number);
    }
}
int polynomial::degree()
{
    int max = 0;
    std::vector<polyNumber> result = bag.toVector();
    for (int i = 0; i < result.size(); i++)
    {
        if (result[i].getDegree() > max)
        {
            max = result[i].getDegree();
        }
    }
    return max;
}
int polynomial::coefficient(int power)
{
    int result = 0;
    std::vector<polyNumber> powerOf = bag.toVector();
    for (int i = 0; i < powerOf.size(); i++)
    {
        if (powerOf[i].getDegree() == power)
        {
            result = powerOf[i].getCoefficient();
        }
    }
    return result;
}
bool polynomial::changeCoefficient(int newCoefficient,int power)
{
    polyNumber number = { newCoefficient,power };
    int result = coefficient(power) + newCoefficient;
    bag.remove(number);
    number.setCoefficient(result);
    bag.add(number);
    return true;
}

解决方法

您有#include "PolyNumber.cpp"而不是#include "PolyNumber.h"

这使得PolyNumber.cpp中定义的所有方法都包含(并重新定义)在Polynomial.cpp中。 LinkedBag也会发生同样的情况。

您为什么要包括cpp个?