I am getting an error "unresolved external symbol" in my code. This is specifically taking place in my setCoefficient() function in my .cpp file.
I am trying to create the representation of a polynomial using dynamic arrays. The way I have things setup is that the index of the array corresponds to the power of the term and the value stored at that particular index is the coefficient for that term.
In my setCoefficient() function, I am trying to determine whether or not I need to resize my array based on if the user is trying to add a coefficient/power that is larger than what my array can currently hold. If I determine I need to re-size my array, I create a new array and copy the contents of the old array into the new, larger, array. However, when I tried to compile my function I ran into the following error:
"Error LNK2019 unresolved external symbol "public: __thiscall Poly::Poly(class Poly const &)" (??0Poly@@QAE@ABV0@@Z) re"
and am not sure how to work around it. If I comment out my setCoefficient() function, the error goes away, so I am certain that the error stems from that function. Can someone help me identify what in my function is causing that error?
Here is my .cpp implementation file:
#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;
}
}
Here is my .h file for this project:
//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