Передо мной стоит задача: «Разработать параметризованный класс для представления матрицы произвольных размеров. Создать конструкторы и деструктор, перегрузить операции сложения, вычитания и умножения (согласно правилам работы с матрицами), обращение по индексу, ввода из потока и вывода в поток. Создать собственные классы исключений и генерировать соответствующие объекты-исключения, если невозможно выполнить ту или иную операцию.»
Я написал следующий код:
// matrix.h
#pragma once
#ifndef MATRIX_H
#define MATRIX_H
template <typename Type>
class Matrix {
private:
int rowCount;
int columnCount;
int size;
Matrix *ptr;
public:
Matrix();
Matrix(int, int);
~Matrix();
void setMatrix();
void getMatrix();
Type* operator [](int);
Matrix operator+(const Matrix &);
Matrix operator-(const Matrix &);
Matrix operator *(const Matrix &);
Type *minPositive();
};
#endif
// matrix.cpp
#include "stdafx.h"
#include "matrix.h"
#include <iomanip>
using namespace std;
template <typename Type>
Matrix<Type>::Matrix() {
printf("Введите кол-во строк матрицы: ");
cin >> rowCount;
printf("Введите кол-во столбцов матрицы: ");
cin >> columnCount;
Type **prt = new Type*[rowCount];
for (int i = 0; i < rowCount; i++) {
ptr[i] = new Type[columnCount];
}
}
template <typename Type>
Matrix<Type>::Matrix(int rowSize, int columnSize) {
rowCount = rowSize;
columnCount = columnSize;
Type **arrayPointer = new Type*[rowCount];
for (int i = 0; i < rowCount; i++) {
ptr[i] = new Type[columnCount];
}
}
template <typename Type>
Matrix<Type>::~Matrix() {
delete[] ptr;
}
template <typename Type>
void Matrix<Type>::setMatrix() {
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
ptr[i][j] = rand() % 50;
}
}
}
template <typename Type>
void Matrix<Type>::getMatrix() {
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < columnCount; j++) {
cout << setw(4) << ptr[i][j] << endl;
}
}
}
template <typename Type>
Type*::operator [](int subscript) {
size = rowCount + columnCount;
if (subscript < 0 || subscript >= size) {
cerr << "\n Ошибка индекса: " << subscript << endl;
exit(1);
}
return ptr[subscript];
}
template <typename Type>
Matrix<Type> Matrix<Type>::operator +(const Matrix<Type> &M) {
return Matrix<Type>(*this).operator+=(M);
}
template <typename Type>
Matrix<Type> Matrix<Type>::operator -(const Matrix<Type> &M) {
return Matrix<Type>(*this).operator-=(M);
}
template <typename Type>
Matrix<Type> Matrix<Type>::operator *(const Matrix<Type> &M) {
return Matrix<Type>(*this).operator*=(M);
}
// main.cpp
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <iomanip>
#include <conio.h>
#include "matrix.h"
using namespace std;
void main() {
setlocale(LC_ALL, "rus");
try {
Matrix<double> matrix;
matrix.setMatrix();
matrix.getMatrix();
}
catch (const exception&) {
}
_getch();
}
Visual studio выдает ошибку компиляции:
Ошибка LNK2019 ссылка на неразрешенный внешний символ "public: __thiscall Matrix<double>::Matrix<double>(void)" (??0?$Matrix@N@@QAE@XZ) в функции _main
Что не так или где ошибка в коде?