- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我使用的是 Visual Studio Professional 2013。我遇到了一个相当奇怪的问题。通常人们发布有关出错的信息 - 我在这里发布有关未出错的信息。
我已经编写了一个自定义 Matrix 类(用于家庭作业)。我已经重写了赋值运算符,如下所示:
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;
}
我最近更改了 newMatrixArray() 方法以接受 bool 参数:
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];
}
}
}
但是,Visual Studio 仍然编译成功......除非
#include "Matrix.h"
int main() {
Matrix<int> matrix;
Matrix<int> otherMatrix;
otherMatrix = matrix;
return 0;
}
我编写了一些使用重载赋值运算符的代码。这让我很担心,因为现在我不知道还有什么可能会被破坏,而且 Visual Studio 也没有告诉我!
这是怎么回事?
更多信息:
如您所见,我正在使用模板。所有 Matrix 代码都在 Matrix.h 文件中 - 声明后跟定义。这在使用模板时是必需的。除了 main.cpp 文件之外,Matrix 类是我现在项目中唯一的类。我已检查并确保声明和定义匹配。
图片来源:Praetorian
编辑:(解决方案)
您可以使用:
template class NameOfClass<NameOfType>;
针对特定类型编译模板类。
您还可以使用:
template ReturnType NameOfFunction(Args ... );
使用模板参数编译类外方法。
这些应该放在全局范围内。
最佳答案
你说:
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?
编译器所做的没有任何问题。如果未使用类模板的成员函数,则该函数不会被实例化。无论函数是否被实例化,都会报告一些错误,例如括号不匹配,但其他错误,如您提到的错误,除非函数被实例化,否则不会报告。
关于c++ - Visual Studio 2013 编译成功但有明显错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621718/
通常当我请求线程转储时,系统性能不佳的症状很容易解释;也就是说,通常我会看到许多线程显然正在等待一个已被获取但未被另一个释放的监视器。 在这种情况下,我有很多线程在等待监视器 (0x965ad100)
C:\Users\shagy\Desktop\3RD YEAR 2ND SEMESTER\SPM\Newfolder\SPM-SMS>npm start npm ERR! path C:\Users\
我是一名优秀的程序员,十分优秀!