could not convert {...} from <brace-enclosed initializer list> to struct
Solution 1
The problem is here:
struct Test
{
int x = 0; // <==
Vector2 v;
};
Until recently, default member initializer prevent the class from being an aggregate, so you cannot use aggregate initialization on them. Gcc 4.9 still implements the old rules here, whereas gcc 5 uses the new ones.
Solution 2
You missed ;
after your class definition and after int x = 0
. Then you got many errors and apparently only considered the last one. But your compiler was confused because Vector2
was not defined (due to missing ;
).
This compiles:
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;
}
Solution 3
// 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() {}
I tried this similar example from the book elements of programming interview
. It shows the same problem and the answer above is not quite applicable to this case. I am using g++ -std=c++11 with gcc version 8.3.0 (Ubuntu 8.3.0-6ubuntu1)
GamesTable Studio
Updated on October 14, 2020Comments
-
GamesTable Studio over 3 years
I'd previously used TDM-GCC-5.10 and now switched back to 4.9 MINGW-GCC and getting a weird error with trying to use list-initialization:
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; }
Error:
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 ^
I used C++14 with both compilers. What is wrong?