简体   繁体   中英

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.

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.

Maybe I should have posted my entire code so one of you could tell me that I :

A . Will receive an error for redefining the class in my *.cpp file, and

B . Needed to include my *.h file in the *.cpp file so it knows where to get the class declaration. I'm sorry for wasting your time with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM