0

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;
    }
}
  • Without looking much further, I can't see a definition for the copy constructor, just the declaration. – olepinto Jan 08 '21 at 08:11
  • The explaination for the error has been given, but I think you should think about why you are returning a `Poly` object from `setCoefficient`, that doesn't make any sense. `setCofficient` should set a coefficient on the existing object, and if it needs to allocate a new `polyArray` it should do it in the existing object, not create a totally new object. – john Jan 08 '21 at 08:16
  • hi @john, I see what you are saying, but how would I be able to re-size my array if the user wanted to add a term that is larger than the length of the array? – Aviv Weinstein Jan 09 '21 at 23:16
  • @AvivWeinstein You would have to allocate a new larger block of memory, copy over the old terms to the new array, then delete the old array. That's similar to what you are doing now, but do it in the same object not in a totally new object. Dynamic arrays are complicated and tricky to do right, this is why you should be using a `std::vector` instead of an array. – john Jan 10 '21 at 06:30
  • @john, I see what you mean. Instead of creating a new Poly object, I am trying to create a new dynamic array(largerArray) that is properly sized -> copy the contents of the current object dynamic array to largerArray -> add the term that was too large for the original Poly object to largerArray. I then delete the polyArray from the current Poly object, resize the polyArray, and try to assign the contents of largerArray to polyArray. However, I am running into some issues. I wish I could use the vector class, but I am not allowed to for this assignment. – Aviv Weinstein Jan 10 '21 at 23:46
  • @AvivWeinstein That's more or less it. If you are still having issues then you can always ask a new question. – john Jan 11 '21 at 05:55

1 Answers1

1

You have declared Poly::Poly(const Poly& p); in your header file (which is supposed to create a deep copy of a Poly) but you haven't defined it in your .cpp file.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108