Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2013 compiling successfully but with obvious error

I'm using Visual Studio Professional 2013. I have a rather odd problem. Normally people post about getting errors - I'm here posting about NOT getting errors.

I have written a custom Matrix class (for homework). I have overriden the assignment operator as follows:

template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix<T> &other) {
    if (this != &other) {
        if (this->mRows != other.mRows || this->nColumns != other.nColumns) {
            deleteMatrixArray();
            this->mRows = other.mRows;
            this->nColumns = other.nColumns;
            newMatrixArray();
        } // else reuse the existing array
        // copy contents
        for (unsigned int i = 0; i < this->mRows; i++) {
            for (unsigned int j = 0; j < this->nColumns; j++) {
                this->matrix[i][j] = other.matrix[i][j];
            }
        }
    }
    return *this;
}

I recently changed the newMatrixArray() method to accept a bool parameter:

template<typename T>
void Matrix<T>::newMatrixArray(bool init) {
    this->matrix = new T*[this->mRows];
    for (unsigned int i = 0; i < this->mRows; i++) {
        if (init) {
            this->matrix[i] = new T[this->nColumns]();
        } else {
            this->matrix[i] = new T[this->nColumns];
        }
    }
}

However, Visual Studio still compiles successfully... UNLESS

#include "Matrix.h"

int main() {

    Matrix<int> matrix;

    Matrix<int> otherMatrix;
    otherMatrix = matrix;

    return 0;
}

I write some code that uses the overloaded assignment operator. This is worrying me because now I don't know what else could be broken and Visual Studio isn't telling me!

What's going on with this?

More information:
As you can see, I am using templates. All the Matrix code is in the Matrix.h file - declaration followed by definition. This is required when using templates. The Matrix class is the only class I have right now in my project besides the main.cpp file. I have checked and made sure the declaration and definition match.

Credit: Praetorian
Edit: (SOLUTION)
You can use:

template class NameOfClass<NameOfType>;

to compile template classes against a certain type.

You can also use:

template ReturnType NameOfFunction(Args ... );

to compile outside-of-class methods with template arguments.

These should be placed in the global scope.

like image 631
Bradley Odell Avatar asked Apr 06 '26 13:04

Bradley Odell


1 Answers

You said:

This is worrying me because now I don't know what else could be broken and Visual Studio isn't telling me!

What's going on with this?

There is nothing wrong with what the compiler is doing. If a member function of a class template is not used, the function doesn't get instantiated. Some errors will be reported regardless of whether the function is instantiated or not, like mismatched parentheses, but other errors, like the one you mentioned won't be reported unless the function is instantiated.

like image 185
R Sahu Avatar answered Apr 09 '26 02:04

R Sahu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!