Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/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++11 使用C++;使用:我在这里做错了什么?_C++11_Type Alias - Fatal编程技术网

C++11 使用C++;使用:我在这里做错了什么?

C++11 使用C++;使用:我在这里做错了什么?,c++11,type-alias,C++11,Type Alias,以下代码未在最新的Microsoft Visual Studio上编译。有人能告诉我我做错了什么吗 #include <iostream> #include <iomanip> #include <array> template <typename T, std::size_t M, std::size_t N> using Matrix = std::array<T, M * N>; template <typename T

以下代码未在最新的Microsoft Visual Studio上编译。有人能告诉我我做错了什么吗

#include <iostream>
#include <iomanip>
#include <array>

template <typename T, std::size_t M, std::size_t N>
using Matrix = std::array<T, M * N>;

template <typename T, std::size_t M, std::size_t N>
std::ostream &operator<<(std::ostream &os, const Matrix<T, M, N> &matrix)
{
    for (auto i = 0; i < M; ++i)
    {
        for (auto j = 0; j < N; ++j)
        {
            os << std::setw(5) << matrix[i * N + j];
        }

        os << std::endl;
    }

    return os;
}

int main(int argc, const char * const argv[])
{
    Matrix<float, 2, 3> matrix{
        1.1f, 1.2f, 1.3f,
        2.1f, 2.2f, 2.3f
    };

    std::cout << matrix << std::endl;

    return 0;
}
#包括
#包括
#包括
模板
使用矩阵=std::数组;
模板

std::ostream&operator记住@dyp的注释,这里要做的是创建新的类型,而不是将有两个独立参数的别名

因此,您只需使用包含实际数据的聚合作为字段,如:

template <typename T, std::size_t M, std::size_t N>
class Matrix
{
private:
    std::array<T, M * N> _data;
    template <typename T1, std::size_t M1, std::size_t N1> friend std::ostream &operator<<(std::ostream &os, const Matrix<T1, M1, N1> &matrix);
public:
    template <typename...Args>
    Matrix(Args... args):
        _data{{std::forward<Args>(args)...}}
    {}
};
模板
类矩阵
{
私人:
std::数组_数据;

template friend std::ostream&Operator在您的
operator@dyp,也许你应该做个回答谢谢,我现在看清楚了!相似
#include <iostream>
#include <iomanip>
#include <array>

template <typename T, std::size_t M, std::size_t N>
using Matrix = std::array<std::array<T, N>, M>;

template <typename T, std::size_t M, std::size_t N>
std::ostream &operator<<(std::ostream &os, const Matrix<T, M, N> &matrix)
{
    for (auto row : matrix)
    {
        for (auto element : row)
        {
            os << std::setw(5) << element;
        }

        os << std::endl;
    }

    return os;
}

int main(int argc, const char * const argv[])
{
    Matrix<float, 2, 3> matrix{
        1.1f, 1.2f, 1.3f,
        2.1f, 2.2f, 2.3f
    };

    std::cout << matrix << std::endl;

    return 0;
}
template <typename T, std::size_t M, std::size_t N>
class Matrix
{
private:
    std::array<T, M * N> _data;
    template <typename T1, std::size_t M1, std::size_t N1> friend std::ostream &operator<<(std::ostream &os, const Matrix<T1, M1, N1> &matrix);
public:
    template <typename...Args>
    Matrix(Args... args):
        _data{{std::forward<Args>(args)...}}
    {}
};