Error Line 31

Write your question here.
The program complies and executes. However, the IDE says for line 31 "error: braces cannot be omitted for this subject initializer" See boldface 1L in line 31.

Why is this happening?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//	Ex10_04
//	Using array containers

#include <iostream>
#include <iomanip>
#include <array>
#include <numeric>

using std::array;

//	List array container contents
template<class T, size_t N>
void listValues(const array<T, N>& data)
{
	const int values_per_line{ 6 };
	int count{};
	
	for (const auto& value : data)
	{
		std::cout << std::setw(14) << value;
		if (++count % values_per_line == 0)
			std::cout << std::endl;
	}
	std::cout << std::endl;
}

int main()
{
	//	Create the famous Fibonacci series
	const size_t N {20};
	array<long, N> fibonacci{ 1L, 1L };
	for (size_t i{ 2 }; i < fibonacci.size(); ++i)
			fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];

	std::cout << "Fibonacci series is: " << std::endl;
	listValues(fibonacci);
	
	array<long, N> numbers;
	numbers.fill(99L);
	fibonacci.swap(numbers);
	std::cout << std::endl << "After swap fabonacci contains." << std::endl;
	listValues(fibonacci);

	//	Create the series for pi/4
	array<double, 120> series;
	double factor{ -1.0 };
	for (size_t x{}; x < series.size(); ++x)
	{
		factor *= -1.0;
		series[x] = factor / (2 * x + 1);
	}
	std::cout << std::endl << "Series for pi is: " << std::endl;
	listValues(series);
	double result{ std::accumulate(cbegin(series), cend(series), 0.0) };
	std::cout << "The series sum converges slowly to pi/4.  The sum x 4 is: "
		<< 4.0 * result << std::endl;

	return 0;
}
Are you sure you are not just executing an older version of the program that was compiled without the error?

I don't see why you should get the error message that you mention. What compiler and what version do you use?
std::array<long, N> fibonacci{ 1L, 1L }; is direct initialisation which requires brace-elision. This is allowed in C++14.

In C++11, brace-elision is allowed only for aggregate initialization with =

std::array<long, N> fibonacci = { 1L, 1L };
Brace elision is probably a good keyword to search with. C++11 and C++14 differ a bit.
array<long, N> fibonacci {{ 1L, 1L }};

In principle you either have a compilation error or compilation succeeds. It sounds like the IDE and the compiler support different versions of C++. (I prefer an editor that simply shows the errors that the compiler spits out; no redundancy that could conflict.)
In practice, all mainstream C++11 compilers decided to allow direct initialisation with brace-elision (typically pass the code with a warning).
Thank you all for your response. I feel honored to receive the attention of highly experienced and knowledgeable C++ programmers.

I first used the statement std::array<long, N> fibonacci = { 1L, 1L };, and the error went away. The program compiled and executed.

I then did a search for Brace elision and came up with this statement:
std::array<long, N> Fibonacci {{ 1L, 1L }};, and the error went away. Of course, the program complied and executed.

I am using Microsoft Visual Studio Express 2013 for Windows Desktop.

Thank you all again for helping me understand the intricacies of C++.


Topic archived. No new replies allowed.