Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++ 错误C2143和错误C2059缺失“&引用;“之前”;{quot;_C++_Arrays_Visual C++_Multidimensional Array - Fatal编程技术网

C++ 错误C2143和错误C2059缺失“&引用;“之前”;{quot;

C++ 错误C2143和错误C2059缺失“&引用;“之前”;{quot;,c++,arrays,visual-c++,multidimensional-array,C++,Arrays,Visual C++,Multidimensional Array,我无法编译它。我不知道发生了什么,它似乎对我很好。我得到的错误是: error C2059: syntax error "{" error C2143: syntax error: missing ";" before "}" error C2143: syntax error: missing ";" before "}" X.h X.cpp 哪里有问题?您的选项卡[4][3]={{0,1,0},{1,0,1},{2,-1,0},{3,0,-1};有几个问题 选项卡[4][3]尝试引用选项卡

我无法编译它。我不知道发生了什么,它似乎对我很好。我得到的错误是:

error C2059: syntax error "{"
error C2143: syntax error: missing ";" before "}"
error C2143: syntax error: missing ";" before "}"
X.h

X.cpp


哪里有问题?

您的
选项卡[4][3]={{0,1,0},{1,0,1},{2,-1,0},{3,0,-1};
有几个问题

  • 选项卡[4][3]
    尝试引用
    选项卡的一个元素,而不是整个数组
  • 它的索引超出了范围--
    tab
    定义为
    tab[4][3]
    ,合法索引从
    tab[0][0]
    tab[3][2]
    运行
  • 您正在尝试分配一个数组,这是不可能的(即使您修复了前面的问题)

  • 您的
    选项卡[4][3]={{0,1,0},{1,0,1},{2,-1,0},{3,0,-1};
    有几个问题

  • 选项卡[4][3]
    尝试引用
    选项卡的一个元素,而不是整个数组
  • 它的索引超出了范围--
    tab
    定义为
    tab[4][3]
    ,合法索引从
    tab[0][0]
    tab[3][2]
    运行
  • 您正在尝试分配一个数组,这是不可能的(即使您修复了前面的问题)

  • 如果C++11适用于您,则可以使用此语法

    X() : tab {{0,1,0},{1,0,1},{2,-1,0},{3,0,-1}} {}
    

    如果C++11适用于您,则可以使用此语法

    X() : tab {{0,1,0},{1,0,1},{2,-1,0},{3,0,-1}} {}
    

    你想做的事情基本上可以用C++ 11兼容的C++编译器来完成。很不幸的是你使用Visual C++(不确定哪个版本),但是这种东西在任何VC++的官方发布中都不能工作。如果VC++完全兼容C++ 11,那么这将是有效的:

    private:
        int tab[4][3] {{0,1,0},{1,0,1},{2,-1,0},{3,0,-1}};
    };
    
    碰巧下面的代码确实适用于符合C++11标准的编译器,但从VS2013开始,它也适用于VC++。使用
    std::array
    可能更适合您:

    #include <array>
    
    class X {
      friend symbol;
      public:
        X();
      private:
        // Create int array with 4 columns and 3 rows.
        std::array<std::array<int, 3>, 4> tab;
    };
    
    
    X::X()
    {
        // Now initialize each row
        tab[0] = { {0, 1, 0} };   // These assignments will not work on Visual studio C++
        tab[1] = { {1, 0, 1} };   //     earlier than VS2013
        tab[2] = { {2, -1, 0} };  // Note the outter { } is used to denote
        tab[3] = { {3, 0, -1} };  //     class initialization (std::arrays is a class)
    
        /* This should also work with VS2013 C++ */
        tab = { { { 0, 1, 0 }, 
                  { 1, 0, 1 }, 
                  { 2, -1, 0 }, 
                  { 3, 0, -1 } } };
    }
    

    你想做的事情基本上可以用C++ 11兼容的C++编译器来完成。很不幸的是你使用Visual C++(不确定哪个版本),但是这种东西在任何VC++的官方发布中都不能工作。如果VC++完全兼容C++ 11,那么这将是有效的:

    private:
        int tab[4][3] {{0,1,0},{1,0,1},{2,-1,0},{3,0,-1}};
    };
    
    碰巧下面的代码确实适用于符合C++11标准的编译器,但从VS2013开始,它也适用于VC++。使用
    std::array
    可能更适合您:

    #include <array>
    
    class X {
      friend symbol;
      public:
        X();
      private:
        // Create int array with 4 columns and 3 rows.
        std::array<std::array<int, 3>, 4> tab;
    };
    
    
    X::X()
    {
        // Now initialize each row
        tab[0] = { {0, 1, 0} };   // These assignments will not work on Visual studio C++
        tab[1] = { {1, 0, 1} };   //     earlier than VS2013
        tab[2] = { {2, -1, 0} };  // Note the outter { } is used to denote
        tab[3] = { {3, 0, -1} };  //     class initialization (std::arrays is a class)
    
        /* This should also work with VS2013 C++ */
        tab = { { { 0, 1, 0 }, 
                  { 1, 0, 1 }, 
                  { 2, -1, 0 }, 
                  { 3, 0, -1 } } };
    }
    

    VisualC++ 2013(官方发布)还完全符合C++ 11,这是失败的。VS C++的以前版本是不太符合Visual C++ 2013(官方发布)的。但是,VS C++的以前版本不太符合VC++使用的版本吗?VC++ 6,VS2005 C++,VS2010 C++等等。你使用的是什么版本的VC++?VC++ 11,VS2005 C++,VS2010 C++等等。