Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++_C++11 - Fatal编程技术网

C++ 运算符重载模板参数

C++ 运算符重载模板参数,c++,c++11,C++,C++11,我有一个实现矩阵的小类。每件事都很顺利,除了给了我在这里发帖的理由。我已经在实际代码中用注释解释了更多关于这个问题的内容。提前感谢任何能提供帮助的人!这不是整个程序,但它足够大,可以自己编译 #include <iostream> #include <initializer_list> template<class type_t, unsigned Rows, unsigned Columns> class matrix { private: std

我有一个实现矩阵的小类。每件事都很顺利,除了给了我在这里发帖的理由。我已经在实际代码中用注释解释了更多关于这个问题的内容。提前感谢任何能提供帮助的人!这不是整个程序,但它足够大,可以自己编译

#include <iostream>
#include <initializer_list>

template<class type_t, unsigned Rows, unsigned Columns>
class matrix
{
private:
    std::initializer_list<std::initializer_list<type_t>> elements;

public:
    type_t contents[Rows][Columns];

    matrix() {}

    matrix(const std::initializer_list<std::initializer_list<type_t>> &container)
        : elements(container)
    {
        unsigned i = 0;
        for (const auto &el : elements)
        {
            unsigned j = 0;
            for (const auto &num : el)
            {
                contents[i][j] = num;
                j++;
            }
            i++;
        }
    }

    unsigned rows() { return Rows; }
    unsigned columns() { return Columns; }

    type_t &operator()(const unsigned &i, const unsigned &j)
    {
        return contents[i][j];
    }

    template<unsigned rws, unsigned cls>
    auto operator*(matrix<type_t, rws, cls> &mat)
    {
        matrix<type_t, Rows, 3> ret_mat;  //OK, but only in case the return matrix has 3 columns
    matrix<type_t, Rows, mat.columns()> ret_mat; //Error. This is the desired result
                                //The error message tells me it needs to be a compile-time constant
                                //it's pretty obvious why the first works and what the problem is
                                //but i still have no idea how to get past this

        for (unsigned i = 0; i < this->rows(); ++i)
        {
            for (unsigned j = 0; j < mat.columns(); ++j)
            {
                for (unsigned in = 0; in < 2; ++in)
                    ret_mat.contents[i][j] += this->contents[i][in] * mat.contents[in][j];
            }
        }

        return ret_mat;
    }
};

int main()
{
    matrix<int, 4, 2> mat = { { 7, 3 },{ 2, 5 },{ 6, 8 },{ 9, 0 } };
    matrix<int, 2, 3> mat2 = { { 7, 4, 9 },{ 8, 1, 5 } };

    auto mat3 = mat * mat2;

    for (unsigned i = 0; i < mat3.rows(); ++i)
    {
        for (unsigned j = 0; j < mat3.columns(); ++j)
            std::cout << mat3(i, j) << " ";
        std::cout << std::endl;
    }
    std::cin.get();
}
#包括
#包括
模板
类矩阵
{
私人:
std::初始值设定项\列表元素;
公众:
键入内容[行][列];
矩阵(){}
矩阵(常数标准::初始值设定项列表和容器)
:元素(容器)
{
无符号i=0;
用于(常量自动和el:元素)
{
无符号j=0;
用于(常数自动和数量:el)
{
内容[i][j]=num;
j++;
}
i++;
}
}
无符号行(){返回行;}
无符号列(){return columns;}
类型_t&运算符()(常数无符号&i,常数无符号&j)
{
返回内容[i][j];
}
模板
自动运算符*(矩阵和矩阵)
{
matrix ret_mat;//OK,但仅当返回矩阵有3列时
matrix ret_mat;//错误。这是所需的结果
//错误消息告诉我它必须是编译时常量
//很明显,为什么第一个有效,问题是什么
//但我仍然不知道如何度过这一关
for(无符号i=0;irows();++i)
{
for(无符号j=0;j材料内容[i][in]*材料内容[in][j];
}
}
返回网垫;
}
};
int main()
{
矩阵mat={7,3},{2,5},{6,8},{9,0};
矩阵mat2={7,4,9},{8,1,5};
自动mat3=mat*mat2;
for(无符号i=0;i模板
你已经有了想要的表情

matrix<type_t, Rows, cls> ret;
matrix-ret;
编辑:正如@juanchopanza所提到的,为什么允许N*M在K*L上乘以M!=K?应该是

template<unsigned cls>

auto operator*(matrix<type_t, columns, cls> &mat)
{
    matrix<type_t, Rows, cls> ret_mat;
模板
自动运算符*(矩阵和矩阵)
{
基质网;

是什么阻止了你使用
矩阵ret\u mat;
?我认为你不应该允许两个
N*M
矩阵相乘,因为
N!=M
。数学就是不对!你应该只允许
[L*M]*[M*N]
,这很容易用模板强制执行。你到底为什么要存储一个
初始值设定项列表
std::array
std::vector
将是更好的选择。关键是,要做到这一点,不需要更多的代码。
template<unsigned cls>

auto operator*(matrix<type_t, columns, cls> &mat)
{
    matrix<type_t, Rows, cls> ret_mat;