C++ 无法使用“编译”编译代码;标准::成对“;

C++ 无法使用“编译”编译代码;标准::成对“;,c++,c++11,C++,C++11,我试着像这样学习和修改这个程序 diff --git a/games/connect_four.h b/games/connect_four.h index a575217..52f59cf 100644 --- a/games/connect_four.h +++ b/games/connect_four.h @@ -3,6 +3,7 @@ #include <algorithm> #include <iostream> +#include <utility

我试着像这样学习和修改这个程序

diff --git a/games/connect_four.h b/games/connect_four.h
index a575217..52f59cf 100644
--- a/games/connect_four.h
+++ b/games/connect_four.h
@@ -3,6 +3,7 @@

 #include <algorithm>
 #include <iostream>
+#include <utility>
 using namespace std;

 #include <mcts.h>
@@ -15,6 +16,9 @@ public:

        static const char player_markers[3]; 

+        typedef std::pair <int, int> MyMove;
+        static const MyMove my_no_move (-1, -1);
+
        ConnectFourState(int num_rows_ = 6, int num_cols_ = 7)
                : player_to_move(1),
              num_rows(num_rows_),
然而,在同一台机器上,我测试编译的代码的同一部分

#include <utility>

int main ()
{
    typedef std::pair <int, int> MyMove;
    static const MyMove my_no_move (-1, -1);
    return 0;
}

$ g++ temp.C -std=c++0x
$
#包括
int main()
{
typedef std::pair MyMove;
静态常量MyMove my_no_move(-1,-1);
返回0;
}
$g++temp.C-std=C++0x
$
为什么??我承认我机器上的编译器没有更新,这不完全支持c++11,但是为什么相同的代码行会有不同的结果呢

您可以:

class MyClass {
    static const MyMove MyClass::my_no_move;
};

const MyMove MyClass::my_no_move(-1, -1);

不能声明这样的类的成员变量

声明如下:

typedef std::pair <int, int> MyMove;
static const MyMove my_no_move;

我们甚至不知道你的编译器是什么版本…它们有不同的结果,因为它们实际上不是相同的代码行;第一个是内联成员初始化,第二个是局部变量声明。它们看起来一模一样,但含义完全不同。更新你的编译器。你的测试用例应该有一个类定义中的两条测试线,否则它与原来的版本有很大的不同。请原谅我是C++的新手,但是为什么这条线会起作用:“静态const移动NoIOTROOR= -1;”这又在类声明中?也许在我的例子中,类型不够简单,编译器无法赋值?此外,错误消息听起来更像是一个语法错误,更令人困惑Move是接受单个整数值作为输入的类型时,code>才能编译
std::pair
不满足此要求。如果您使用的是C++11或更高版本,请尝试以下操作:
static const MyMove my_no_move{-1,-1}
typedef std::pair <int, int> MyMove;
static const MyMove my_no_move;
// The typedef MyMove is also scoped
const ConnectFourState::MyMove ConnectFourState::my_no_move(-1, -1);