问题描述
我的代码中出现错误“未解析的外部符号”。这特别发生在我的 .cpp 文件中的 setCoefficient() 函数中。
我正在尝试使用动态数组创建多项式的表示。我设置事物的方式是数组的索引对应于项的幂,并且存储在该特定索引处的值是该项的系数。
在我的 setCoefficient() 函数中,我试图根据用户是否尝试添加大于我的数组当前可以容纳的系数/幂来确定我是否需要调整数组的大小。如果我确定需要重新调整数组大小,我会创建一个新数组并将旧数组的内容复制到新的更大的数组中。但是,当我尝试编译我的函数时遇到了以下错误:
"Error LNK2019 unresolved external symbol "public: __thiscall Poly::Poly(class Poly const &)" (??0Poly@@QAE@ABV0@@Z) re"
并且不确定如何解决它。如果我注释掉我的 setCoefficient() 函数,错误就会消失,所以我确定错误源于该函数。有人可以帮助我确定我的函数中导致该错误的原因吗?
这是我的 .cpp 实现文件:
#include "poly.h"
//Default constructor. Initializes a Poly to: 0x^0
Poly::Poly()
{
cout << "No parameter constructor called" << endl;
polyArray = new int[1];
//polyArray[1] = 0;
polyArray[0] = 0;
length = 0;
}
//Constructor with one argument. Initialized to: coefficientx^0
Poly::Poly(int coefficient)
{
cout << "One parameter constructor called" << endl;
cout << "coefficient: " << coefficient << endl;
polyArray = new int[1];
//polyArray[1] = 0;
polyArray[0] = coefficient;
length = 0;
}
//Constructor with two arguments. Initialized to: coefficientx^power
Poly::Poly(int coefficient,int power)
{
cout << "Two parameter constructor called" << endl;
cout << "power: " << power << endl;
cout << "coeff: " << coefficient << endl;
polyArray = new int[power]; //I think I need to add 1 here to account for that fact that arrays start at index 0.
for (int i = 0; i < power; i++) { //I want to initialize the dynamic array to all 0's
polyArray[i] = 0;
}
polyArray[power] = coefficient;
length = power;
}
//Destructor
Poly::~Poly()
{
cout << "I am the destructor" << endl;
polyArray = NULL;
delete [] polyArray;
}
ostream& operator<<(ostream& out,const Poly& p)
{
for (int i=p.length; i>=0; i--) {
if (p.polyArray[i] == 0 && i == 0 && p.length == 0) {
cout << "0";
}
else if (p.polyArray[i] == 0) {
cout << "";
}
else if(i==0 && p.polyArray!=0){
cout << p.polyArray[i];
}
else {
cout << p.polyArray[i] << "^" << i;
}
}
return out;
}
Poly Poly::setCoefficient(const int coefficient,const int power) {
if (power > this->length){
Poly largerPolyArray(0,power * 2);
largerPolyArray.length = power * 2;
//Here I am creating a dynamic array that is twice as large as the power.
//This is wasteful,but it works
for (int i = 0; i < this->length; i++) {
largerPolyArray.polyArray[i] = this->polyArray[i];
}
largerPolyArray.polyArray[power] = coefficient;
return largerPolyArray;
}
else {
this->polyArray[power] = coefficient;
return *this;
}
}
这是我的这个项目的 .h 文件:
//A Poly class
#ifndef Poly_H
#define Poly_H
#include <iostream>
#include <string>
using namespace std;
class Poly {
friend ostream& operator<<(ostream& out,const Poly& p);
friend istream& operator>>(istream& in,Poly& p);
public:
//Member functions
Poly(); //Default constructor. Initializes a Poly to: 0x^0
Poly(int coefficient); //Constructor with one argument. Initialized to: coefficientx^0
Poly(int coefficient,int power); //Constructor with two arguments. Initialized to: coefficientx^power
Poly(const Poly& p); //Copy constructor. Creates a deep copy of a Poly.
~Poly(); //Destructor
int degree() const; //Returns the largest degree term in a Poly.
double getCoefficient(const int power) const; //returns the coefficient of the x^power term.
Poly setCoefficient(const int coefficient,const int power); //Sets the coefficient of the term indicated by the power.
// Arithmetic operators
Poly operator+(const Poly& p) const;
Poly operator-(const Poly& p) const;
// Boolean comparison operators
bool operator==(const Poly& p) const;
bool operator!=(const Poly& p) const;
// Assignment operators
Poly& operator=(const Poly& p);
Poly& operator+=(const Poly& p);
Poly& operator-=(const Poly& p);
Poly& operator*=(const Poly& p);
//Clean up/delete all Poly terms.
void clear();
//Checks if the Poly is empty or not.
bool isEmpty() const;
private:
int length; //# of terms in the Poly/size of the array
int* polyArray;
};
#endif
解决方法
您已经在头文件中声明了 Poly::Poly(const Poly& p);
(它应该创建一个 Poly
的深层副本),但是您还没有在 .cpp
文件中定义它。>