我在理解我设置指针的方式有什么问题时遇到了问题。我正在尝试按照以下方式做一些事情:
matrix[0] = new Array<T>(columnSize);
但它不编译。我认为问题在于 matrix[0] 返回一个引用,并且将 new Array(columnSize) 分配给它是不合法的?
这是错误
/Users/Ezequiel/desktop/wow/Matrix.h: In constructor ‘Matrix<T>::Matrix(size_t, size_t) [with T = int]’:
main.cpp:8: instantiated from here
/Users/Ezequiel/desktop/wow/Matrix.h:18: error: invalid conversion from ‘Array<int>*’ to ‘long unsigned int’
/Users/Ezequiel/desktop/wow/Matrix.h:18: error: initializing argument 1 of ‘Array<T>::Array(size_t) [with T = Array<int>*]’
我在下面附上了我的所有代码。
// Array.h
#ifndef ARRAY_H
#define ARRAY_H
#include <string>
#include <iostream>
#include <algorithm>
template<class T>
class Array {
private:
T* elements;
size_t low, high;
void init(const size_t, const size_t);
void copy(const Array<T>&);
public:
size_t size;
Array();
~Array();
Array(const size_t);
Array(const size_t, const size_t);
Array(const Array<T>&);
T& operator[](const size_t);
Array<T>& operator=(const Array<T>&);
template<class X>
friend std::ostream& operator<<(std::ostream&, const Array<X>&);
};
template<class T>
void Array<T>::init(const size_t low, const size_t high) {
if (high < low || (high - low + 1 <= 0)) {
std::cout << "An error was found in the bounds passed to the constructor.\n";
exit(1);
}
size = high - low + 1;
elements = new T[size];
this->low = low;
this->high = high;
}
template<class T>
void Array<T>::copy(const Array<T>& someArray) {
delete[] elements;
if (someArray.size) {
elements = new T[someArray.size];
std::copy(someArray.elements, someArray.elements + someArray.size, elements);
low = someArray.low;
high = someArray.high;
size = someArray.size;
} else {
size = 0;
elements = 0;
low = high = -1;
}
}
template<class T>
Array<T>::Array() {
size = 0;
elements = 0;
low = high = -1;
}
template<class T>
Array<T>::Array(const size_t size) {
init(0, size - 1);
}
template<class T>
Array<T>::Array(const size_t low, const size_t high) {
init(low, high);
}
template<class T>
Array<T>::~Array() {
delete[] elements;
}
template<class T>
Array<T>::Array(const Array<T>& someArray) {
copy(someArray);
}
template<class T>
Array<T>& Array<T>::operator=(const Array& someArray) {
if (this == &someArray) {
return *this;
}
copy(someArray);
return *this;
}
template<class T>
T& Array<T>::operator[](const size_t index) {
if (index < low || index > high || size <= 0) {
std::cout << "The index " << index << " goes out of bounds.\n";
exit(1);
}
return elements[index - low];
}
template<class X>
std::ostream& operator<<(std::ostream& os, const Array<X>& someArray) {
if (someArray.elements) {
for (size_t i = 0; i < someArray.size - 1; ++i) {
os << someArray.elements[i] << ", ";
}
os << someArray.elements[someArray.size - 1];
} else {
os << 0;
}
}
#endif
// Matrix.h
#include "Array.h"
template<class T>
class Matrix {
private:
Array< Array<T>* > *matrix;
size_t rowSize, columnSize;
public:
Matrix(const size_t rowSize, const size_t columnSize);
};
template<class T>
Matrix<T>::Matrix(const size_t rowSize, const size_t columnSize) {
matrix = new Array< Array<T>* >(rowSize);
// Compiles perfectly fine, except after I uncomment this line:
// matrix[0] = new Array<T>(columnSize);
this->rowSize = rowSize;
this->columnSize = columnSize;
}