Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.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++_Visual Studio_Templates - Fatal编程技术网

为什么这个C++数组模板类用垃圾值初始化?

为什么这个C++数组模板类用垃圾值初始化?,c++,visual-studio,templates,C++,Visual Studio,Templates,使用Visual Studio 2019,我试图创建一个模板数组类,其中包含数组的类型及其大小: // Array.hpp #include <iostream> template<typename T, int S> class Array { T m_array[S]; public: Array() : Array(0) {} Array(const T& val) { for (int i = 0;

使用Visual Studio 2019,我试图创建一个模板数组类,其中包含数组的类型及其大小:

// Array.hpp
#include <iostream>
template<typename T, int S> class Array {

    T m_array[S];
    
public:

    Array() : Array(0) {}

    Array(const T& val) {
        for (int i = 0; i < S; i++) { m_array[i] = val; }
    }

    template<typename E> Array(const E& val) {
        for (int i = 0; i < S; i++) { m_array[i] = static_cast<T>(val); }
    }

    int size() const { return S; }
    T operator[](int position) const { return m_array[position]; }
    T& operator[](int position) { return m_array[position]; }

    friend std::ostream& operator<<(std::ostream& os, const Array& a) {
        os << '[';
        if (a.size() > 0) { os << a[0]; }
        for (int i = 1; i < a.size(); i++) {
            os << ', ' << a[i];
        }
        return os << ']';
    }
};
给我这个输出:

这:

是多字符文字。那根本不是你想要的。替换为:

", "

如果可以,在编译器中为多字符文本打开警告。在GCC中,即-Wmultichar.

请不要将文本作为图像发布。“,”->,
', '
", "