Search code examples
c++lnk2019

LNK2019 error when linking CPP file for tests


I am trying to write a Cubic class that performs operations on polynomials of degree 3. When attempting to overload the + operator to return a new Cubic object, it gives me a LNK2019 error:

"unresolved external symbol "public: __thiscall Cubic::Cubic(void)" (??0Cubic@@QAE@XZ) referenced in function "public: class Cubic const __thiscall Cubic::operator+(class Cubic)" (??HCubic@@QAE?BV0@V0@@Z)"

I've tried looking at whether my function declarations are different from my definitions but they are all the same. I believe the problem is in the overloaded operator because I tried modifying each coefficient using rhs.coefficient[i] += coefficient[i] and returning rhs and that worked fine. The reason why I want to return a new Cubic is because it seems more correct and also because it would make it easier to implement the - operator as well.

Cubic.h file

#include <iostream>

using namespace std;

    class Cubic
    {
    public:
        // Default constructor
        Cubic();
        // Predetermined constructor
        Cubic(int degree3, int degree2, int degree1, int degree0);

        // Return coefficient
        int getCoefficient(int degree) const;

        // Addition op
        const Cubic operator+(Cubic rhs);

        // Output operators
        friend ostream& operator<<(ostream& outStream, const Cubic& cubic);
    private:
        int coefficient[4];
    };

Cubic.cpp

#include "Cubic.h"
#include <iostream>

using namespace std;

Cubic::Cubic(int degree3, int degree2, int degree1, int degree0)
{
    coefficient[3] = degree3;
    coefficient[2] = degree2;
    coefficient[1] = degree1;
    coefficient[0] = degree0;
}

int Cubic::getCoefficient(int degree) const
{
    return coefficient[degree];
}

const Cubic Cubic::operator+(Cubic rhs)
{
    Cubic result;

    for (int i = 3; i >= 0; i--)
    {
        result.coefficient[i] = coefficient[i] + rhs.coefficient[i];
    }

    return result;
}

ostream& operator<<(ostream& outStream, const Cubic& cubic) {
    outStream << showpos << cubic.getCoefficient(3) << "x^(3)"
        << cubic.getCoefficient(2) << "x^(2)"
        << cubic.getCoefficient(1) << "x"
        << cubic.getCoefficient(0);

    return outStream;
}

Test.cpp

#include "Cubic.h"
#include <iostream>

using namespace std;

int main()
{
    Cubic lhs(3, 1, -4, -1);
    Cubic rhs(-1, 7, -2, 3);

    /* TESTS */
    cout << "Addition using + operator" << endl;
    cout << lhs + rhs << endl;

    return 0;
}

The expected result should be +2x^(3)+8x^(2)-6x+2.


Solution

  • Your problem is that you have declared a default constructor for your Cubic class, here:

    // Default constructor
    Cubic();
    

    but you have never defined that constructor (at least, not in any of the code you've shown).

    You need to either define the default constructor inline, like this:

    // Default constructor
    Cubic() { } // This provides a definition, but it doesn't DO anything.
    

    or provide a separate definition elsewhere:

    Cubic::Cubic()
    {
        // Do something here...
    }
    

    You could also add the "Do something" code to the inline definition! Most coders would do this if the definition is small (one or two lines) but keep the definition separate for more complex code.

    EDIT: Alternatively, as @Scheff has pointed out in the comments, you could explicitly define the default constructor as such using:

    // Default constructor
    Cubic() = default; // Will do minimal required construction code.