어떻게 생성자의 2D 배열에 동적 메모리를 할당 할 수 있습니까? 그러나 동시에 std::unique_ptr
의 할당 해제 처리를 유지 하시겠습니까? 아니면 이것을 할 수있는 더 좋은 방법이 있습니까?높이가 상수 표현이 아닙니다.
내 오류는 "높이가 상수 표현이 아닙니다."입니다.
#include <iostream>
#include <vector>
#include <memory>
template<typename T>
class Matrix
{
private:
int Width, Height;
std::unique_ptr<T*> Elements;
public:
Matrix(int Width, int Height);
T* operator[](int Index);
const T* operator[](int Index) const;
};
template<typename T>
Matrix<T>::Matrix(int Width, int Height) : Width(Width), Height(Height), Elements(new T[Width][Height]) {}
template<typename T>
T* Matrix<T>::operator[](int Index) {return Elements[Index];}
int main()
{
Matrix<int> M(4, 4);
std::cout << M[2][2];
}
그래,하지만 사용자가 하나의 행 크기를 조정하고 .. 다른 사람을 떠나하기로 결정 그래서 일반 배열을 사용하는 경우 벡터가 이상한 행렬을 각 행을 추적함으로써 수 있습니다 : 디 – Brandon