C++ 为什么我在尝试重载操作符时会出现链接错误?

C++ 为什么我在尝试重载操作符时会出现链接错误?,c++,operator-overloading,lnk2005,C++,Operator Overloading,Lnk2005,我正在做一个项目练习,我正在修改我创建的一个名为多项式的类的先前项目,以使用链接列表(最初使用的数组)。链接列表使用一个模板,以便可以向其中传递任何类型 我在这个项目练习中遇到的一个问题是,我试图传递一个类型为PolyNumber(来自我创建的类)的对象,而我创建的链接列表包有一个功能,可以比较传递给它的任何项(使用=) 它适用于常规类型,如int和string,但在自定义对象类型时会遇到问题。因此,我想出了如何在PolyNumber类中重载=操作符。当我测试这个类时,它本身是有效的,但是当我将

我正在做一个项目练习,我正在修改我创建的一个名为
多项式
的类的先前项目,以使用链接列表(最初使用的数组)。链接列表使用一个模板,以便可以向其中传递任何类型

我在这个项目练习中遇到的一个问题是,我试图传递一个类型为
PolyNumber
(来自我创建的类)的对象,而我创建的链接列表包有一个功能,可以比较传递给它的任何项(使用
=

它适用于常规类型,如
int
string
,但在自定义对象类型时会遇到问题。因此,我想出了如何在
PolyNumber
类中重载
=
操作符。当我测试这个类时,它本身是有效的,但是当我将这个类型与链接列表实现的
多项式
类一起使用时,多项式类中的每个方法都会出现如下错误:

错误LNK2005“public:u thiscall PolyNumber::PolyNumber(int,int)”(??0 PolyNumber@@QAE@HH@Z) 已在polymone.obj Project11中定义
这是我为这些文件编写的代码,但正如您在代码中所看到的,还有其他与此代码配套的文件,例如链接列表对象的
LinkedBag
,但对于空格,我只包含以下内容:

PolyNumber.h

PolyNumber.cpp

多项式h

#pragma一次
#包括“PolynomialInterface.h”
#包括“LinkedBag.cpp”
#包括“PolyNumber.cpp”
静态常数int多项式_SIZE=10;
类多项式:公共多项式_接口
{
公众:
//空多项式
多项式();
//复制构造函数
多项式(多项式&复制);
/**C使用客户端定义的多项式构造多项式
@param一个不超过多项式大小的非负整数系数数组,数组中的每个系数都有一个对应
到该数组中ceffient位置的相应值*/
多项式(整数系数[多项式大小],整数大小);
int度();
int系数(int幂);
布尔变换系数(整数新系数,整数幂);
私人:
//静态常数int多项式_SIZE=10;
//int多项式[多项式的大小];
背包;
};
多项式

#包括“polynomy.h”
多项式::多项式()
{
}
多项式::多项式(多项式和副本)
{
std::vector copyFrom=copy.bag.toVector();
对于(int i=0;i
您有
#包含“PolyNumber.cpp”
而不是
#包含“PolyNumber.h”

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


为什么要包括
cpp
s?

只需一个简单的例子就可以了;以及用于构建的命令为什么要包含
.cpp
-文件?这根本不是你想要的。事实上,我把它转过来测试它是否有效,然后就这样离开了当我写这篇文章时,我最初有“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;
};
#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);
}
#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;
};
#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;
}