C++ 无法将{…}从<;括号内的初始值设定项列表>;构造

C++ 无法将{…}从<;括号内的初始值设定项列表>;构造,c++,gcc,struct,initialization,C++,Gcc,Struct,Initialization,我以前使用过TDM-GCC-5.10,现在切换回4.9 MINGW-GCC,尝试使用列表初始化时出现了一个奇怪的错误: class Vector2 { public: Vector2(float x, float y) { this->x = x; this->y = y; } float x = 0.f; float y = 0.f; }; struct Test { int x = 0; V

我以前使用过TDM-GCC-5.10,现在切换回4.9 MINGW-GCC,尝试使用列表初始化时出现了一个奇怪的错误:

class Vector2
{
public:
    Vector2(float x, float y)
    {
        this->x = x;
        this->y = y;
    }
    float x = 0.f;
    float y = 0.f;
};

struct Test
{
    int x = 0;
    Vector2 v;
};

int main()
{    
    Test tst = {0,Vector2(0.0f,0.0f)}; //Error
    return 0;
}
错误:

main.cpp: In function 'int main()':
main.cpp:21:41: error: could not convert '{0, Vector2(0.0f, 0.0f)}' from '<brace-enclosed initializer list>' to 'Test'
         Test tst = {0,Vector2(0.0f,0.0f)}; //Error
                                         ^
main.cpp:在函数“int main()”中:
main.cpp:21:41:错误:无法将“{0,Vector2(0.0f,0.0f)}”从“”转换为“Test”
测试tst={0,向量2(0.0f,0.0f)}//错误
^

我在两个编译器中都使用了C++14。怎么了?

您错过了
在类定义之后和
int x=0
之后。然后你犯了很多错误,显然只考虑了最后一个。但是您的编译器感到困惑,因为未定义
Vector2
(由于缺少

这包括:

int main()
{
    class Vector2
    {
    public:
        Vector2(float x, float y)
        {
            this->x = x;
            this->y = y;
        }
        float x = 0.f;
        float y = 0.f;
    };

    struct Test
    {
        int x;
        Vector2 v;
    };

    Test tst = {0,Vector2(4,5)};
    return 0;
}
问题在于:

struct Test
{
    int x = 0; // <==
    Vector2 v;
};
struct测试
{
int x=0;//
///10.3最低公共祖先
//解决方案:蛮力方法是查看节点是否处于不同的位置
//根的直接子树,或者如果其中一个节点是根
#包括
使用名称空间std;
模板
结构节点{
T数据;
唯一左上角;
唯一ptr权限;
};
结构状态{
int num_目标节点;
//节点*祖先;
独特的祖先;
};
状态LCA帮助器(唯一\u ptr&根、唯一\u ptr&节点0、,
唯一节点(ptr&node1){
if(root==nullptr){
返回{0,nullptr};
}
自动l=LCAHeloper(根->左,节点0,节点1);
如果(l.num_target_nodes==2)返回l;
自动r=LCAHeloper(根->右,节点0,节点1);
if(r.num_target_nodes==2)返回r;
int num_target_nodes=l.num_target_nodes+r.num_target_nodes+
(root==node0)+(root==node1);
返回{num_target_nodes,num_target_nodes==2?根:nullptr};
//返回{num_target_nodes,num_target_nodes==2?root.get():
//nullptr};
}
//节点*lca(唯一\u ptr和根、唯一\u ptr和节点0,
//唯一节点(ptr&node1){
唯一的生命周期评价(唯一的生命周期和根),
独特的ptr和node0,
唯一节点(ptr&node1){
返回lcaheloper(root、node0、node1);
}
int main(){}

我在《编程访谈的元素》一书中尝试了这个类似的例子。它显示了同样的问题,上面的答案并不完全适用于这个案例。我使用的是gcc版本8.3.0(Ubuntu 8.3.0-6ubuntu1)的g++-std=c++11

问题中没有
main
。请查看并提供一个@Olaf编辑,只需复制并尝试运行它。回滚到以前的版本。如果编辑删除了答案的上下文,则在获得答案后不应编辑该问题。您可以附加一个解释,但前提是该解释已明确标记为该解释。但答案是没有意义的,因为它与问题无关。@Olaf:我不同意。按照这种逻辑,如果有人回答有拼写错误,任何有拼写错误的问题都应该保留它。抱歉,刚才添加了它们,您使用哪种编译器版本和标准?这不是因为缺少;(我只在本例中忘记了)使用TDM-GCC编译器,一切正常。g++4.8.4带有选项
-std=c++11
。注意,我还(意外地)删除了
=0
到测试的
x
声明。添加它显然会导致编译器错误。如果我删除它编译的任何方言选项,看起来我与编译器有问题,但使用c++11/c++14它会给我带来此错误。难以置信,这甚至会影响前向兼容性。他们不应该做此更改!这也是一个问题m这对VC2017非常有效!@DrumM我不理解这个评论-什么影响兼容性?什么变化永远不应该做?就像你说的,当类不再是聚合时,默认构造函数不会生成。也许说“它不向前兼容”是错误的因为我们正在使用一个新功能。但在我看来,规范很混乱,如果在成员上指定默认初始化,为什么还需要实现默认构造函数…没有意义。@DrumM-Huh?
Test
这里有一个默认构造函数。您还需要实现什么?@DrumM“就像你说的,当类不再是聚合时,默认构造函数就不会生成”——我从来没有这样说过。
// 10.3  lowest common ancestor

// Solution: A brute-force approach is to see if the nodes are in different
// immediate subtrees of the root, or if one of the nodesis the root

#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct Node {
  T data;

  unique_ptr<Node<T>> left;
  unique_ptr<Node<T>> right;
};

struct Status {
  int num_target_nodes;
  //   Node<int>* ancestor;
  unique_ptr<Node<int>> ancestor;
};

Status lcaHelper(unique_ptr<Node<int>>& root, unique_ptr<Node<int>>& node0,
                 unique_ptr<Node<int>>& node1) {
  if (root == nullptr) {
    return {0, nullptr};
  }

  auto l = lcaHelper(root->left, node0, node1);

  if (l.num_target_nodes == 2) return l;

  auto r = lcaHelper(root->right, node0, node1);

  if (r.num_target_nodes == 2) return r;

  int num_target_nodes = l.num_target_nodes + r.num_target_nodes +
                         (root == node0) + (root == node1);

  return {num_target_nodes, num_target_nodes == 2 ? root : nullptr};

  //   return {num_target_nodes, num_target_nodes == 2 ? root.get() :
  //   nullptr};
}

// Node<int>* lca(unique_ptr<Node<int>>& root, unique_ptr<Node<int>>& node0,
//                unique_ptr<Node<int>>& node1) {
unique_ptr<Node<int>> lca(unique_ptr<Node<int>>& root,
                          unique_ptr<Node<int>>& node0,
                          unique_ptr<Node<int>>& node1) {
  return lcaHelper(root, node0, node1).ancestor;
}

int main() {}