Search code examples
c++matrixunresolved-external

C++ Error in object construction. Unresolved External Symbol


I have three files, a main.cpp file meant for testing my functions, a header file for the class, and the cpp file where my objects and members are declared.

I am trying to construct my object Matrix in the main file and receiving error: Error: LNK2019 unresolved external symbol "public: __thiscall Matrix::Matrix(int,int)" (??0Matrix@@QAE@HH@Z) referenced in function _main Matrix Project.

I can only assume this mean there is something wrong with my constructor. I am using it as follows and neither of them work:

    Matrix* matrix = new Matrix(2, 3);
    Matrix test = Matrix(2, 3);

The constructor is declared in the *.h file as follows:

Matrix(int numRows, int numCols);

And then declared in the *.cpp file as follows:

public:
    Matrix(int numRows, int numCols) {
        rows = numRows;
        cols = numCols;
        arr = new double*[numCols];
        for (int i = 0; i < numRows; ++i) {
            for (int x = 0; x < numCols; ++x) {
                arr[i][x] = 0;
            }
        }
    }

Any help is appreciated. I'm sure I'm missing something silly.

Thank you in advance!

[EDIT]

For the sake of information, I figured I'd say that the variables rows, cols, and arr are all declared above the constructor as private members of the class.


Solution

  • In the .cpp file you should put the definition of the method which has been declared in the header (.h) file. Therefore your .cpp file should look like:

    Matrix::Matrix(int numRows, int numCols) { }
    

    What you are doing is replicate the class definition in the .cpp file so that why you get the linking error.