Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回向量成员引用时出错 我在C++中实现了一个模板矩阵类,并且我面临一个操作程序()重载的错误。代码如下:_C++_Templates_C++11 - Fatal编程技术网

返回向量成员引用时出错 我在C++中实现了一个模板矩阵类,并且我面临一个操作程序()重载的错误。代码如下:

返回向量成员引用时出错 我在C++中实现了一个模板矩阵类,并且我面临一个操作程序()重载的错误。代码如下:,c++,templates,c++11,C++,Templates,C++11,Matrix.hpp: template <typename T> class Matrix { std::vector<T>* _data; int _sx; int _sy; public: // CTOR - DTOR Matrix(int sx, int sy); Matrix(Matrix<T> const &other); ~Matrix(); // Operator O

Matrix.hpp:

template <typename T>
class Matrix {
    std::vector<T>* _data;
    int _sx;
    int _sy;

public:
    // CTOR - DTOR

    Matrix(int sx, int sy);
    Matrix(Matrix<T> const &other);
    ~Matrix();

    // Operator Overloads

    Matrix<T>& operator+=(Matrix<T> const &other);
    Matrix<T>& operator-=(Matrix<T> const &other);
    Matrix<T>& operator*=(Matrix<T> const &other);
    Matrix<T>& operator/=(Matrix<T> const &other);
    Matrix<T>& operator=(Matrix<T> const &other);
    T& operator()(int x, int y);


    // Member Functions
    void display() const;
    int getSx() const;
    int getSy() const;

private: // Private Member functions

    bool _rangeCheck(int x, int y) const;
};

#include "../srcs/Matrix.inl"
知道我做错了什么吗

(this->_data[(y * this->_sx) + x]);
您的
数据
是一个指针,因此在其上使用
运算符[]
将访问
数据
作为
std::vector
的数组。替换为:

(*(this->_data)[(y * this->_sx) + x]);

您可以考虑在这里避免指针,或者继承<代码> STD::vector < /代码>:

template <typename T>
class Matrix
{
    std::vector<T> _data;
    //...
};

template <typename T>
class Matrix : private std::vector<T>
{
    //...
};
模板
类矩阵
{
std::vector_数据;
//...
};
模板
类矩阵:私有std::vector
{
//...
};
编辑:

正如molbdnilo在评论中指出的,不建议继承标准容器


请查看以了解原因。

(*此->\u数据)[(y*此->\u sx)+x]
,我将其视为打字错误。但是为什么需要
\u data
作为指针?模板实现在标题中。一份。。。在某处
#包括“matrix.cpp”
@UKMonkey。显然这不是cpp问题中的模板。他包括了cpp。我把它重命名为
inl
。我不建议继承。标准的收藏不是为了继承,甚至不是私人的。@molbdnilo你是对的,我低估了这里的后果。我编辑了我的答案
(*(this->_data)[(y * this->_sx) + x]);
template <typename T>
class Matrix
{
    std::vector<T> _data;
    //...
};

template <typename T>
class Matrix : private std::vector<T>
{
    //...
};