使用 GCC 编译时 C++ 中的未知结构错误

Dav*_* Oh 0 c++ gcc constructor struct g++

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int Answer;
struct _pair {
    struct _pair(int a) : value(a), cnt(1) {}
    unsigned int value;
    unsigned int cnt;
    };


int main(int argc, char** argv)
{

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

此代码出现错误:“错误:在 'int' struct _pair(int a) : value(a), cnt(1) {} 之前预期不合格 id”

在VS2017上没有报错,但是在GCC编译器上就报错了。

Dan*_*man 5

构造函数不需要限定符struct

#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int Answer;
struct _pair {
   _pair(int a) : value(a), cnt(1) {}
    unsigned int value;
    unsigned int cnt;
};
Run Code Online (Sandbox Code Playgroud)