Vector Initialization

Why can't a vector be initialized as follows:
 
  vector<int> ivec = {1,2,3,4};


I get the following error message:

error C2440: 'initializing' : cannot convert from 'initializer-list' to 'Vector<int>'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
Last edited on
Does your compiler support C++11 or C++14?
I have Microsoft Visual Studio Professional 2013 installed. How do you tell if the complier supports C++11?
The syntax is wrong.

1
2
3
4
5
6
7
8
#include <vector>
#include <iostream>

int main(){
	std::vector<int> v{ 1, 2, 3 };
	for (auto i : v)
		std::cout << i << std::endl;
}
I thought C++11 was happy with or without the = ? Well, GCC accepts this with -std=c++11 to disable the Gnu-specfic extensions:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>

int main() {
    std::cout << std::boolalpha;

    std::vector<int> v_v1 = { 1, 2, 3 };
    std::vector<int> v_v2{ 1, 2, 3 };

    std::cout << "(v_v1 == v_v2) = " << (v_v1 == v_v2) << "\n";

    return 0;
}


Andy

Edit

PS Example from: C++11 / Initializer lists
https://en.wikipedia.org/wiki/C%2B%2B11

Standard containers can also be initialized in the following ways:

1
2
3
std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" };
std::vector<std::string> v({ "xyzzy", "plugh", "abracadabra" });
std::vector<std::string> v{ "xyzzy", "plugh", "abracadabra" }; // see "Uniform initialization" below 


Last edited on
Sorry Helios, but the code without the assignment operator does not work either. The book that I am reading supports andywestken's comment that the initialization can be performed with or without the assignment operator.
> I have Microsoft Visual Studio Professional 2013 installed.

Update it: http://www.microsoft.com/en-us/download/details.aspx?id=44921

And this would compile cleanly:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> a { 1,2,3,4 } ; // direct initialisation
    std::vector<int> b = { 1,2,3,4 } ; // copy initialisation
    
    std::cout << "compiler version: " << _MSC_VER 
              << " build: " << _MSC_FULL_VER % 100000 << '\n' ; // 18.00.21005
}

http://rextester.com/KBKP91750
JLBorges,

I downloaded Microsoft Visual Studio 2013 Update 4 from the link supplied. I restarted my computer, ran the program with direct initialization, and the same error occurs.

I then removed from the program the header file used by the book and used the header files that you used. The program works by direct initialization and by copy initialization. Thank you very much.

My complier version is

compiler version: 1800 build: 31101
Press any key to continue . . .
Last edited on
compiler version: 1800 build: 31101 is the compiler which comes with Visual Studio 2013 Update 4.

Just compiled the code on a Windows machine with Visual Studio 2013 Update 4
(compiler version: 1800 build: 31101). (It compiled cleanly).


> error C2440: 'initializing' : cannot convert from 'initializer-list' to 'Vector<int>'

Check the case; it is vector (with a lower case 'v').

Tip: Get rid of the habit of using namespace std; and then using unqualified names for entities which must be in the namespace std.
std::Vector<int> would have given the error: 'Vector' : is not a member of 'std'
JLBorges,

Here's the code from the book's header file that deals with Vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
	typedef typename std::vector<T>::size_type size_type;

	Vector() { }
	explicit Vector(size_type n) :std::vector<T>(n) {}
	Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
	template <class I>
	Vector(I first, I last) : std::vector<T>(first, last) {}

	T& operator[](unsigned int i) // rather than return at(i);
	{
		if (i<0 || this->size() <= i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
	const T& operator[](unsigned int i) const
	{
		if (i<0 || this->size() <= i) throw Range_error(i);
		return std::vector<T>::operator[](i);
	}
};

// disgusting macro hack to get a range checked vector:
#define vector Vector 


Currently, I don't have a clue what this does.
Well, it is missing the constructor which accepts an initialiser list.

With that added:

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
#include <iostream>
#include <vector>
#include <initializer_list> // ***

// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
	typedef typename std::vector<T>::size_type size_type;

	Vector() { }
	explicit Vector(size_type n) :std::vector<T>(n) {}
	Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
	template <class I>
	Vector(I first, I last) : std::vector<T>(first, last) {}

	/////////////  added ///////////////////////////
	Vector( std::initializer_list<T> il ) : std::vector<T>(il) {}
	////////////////////////////////////////////////

	T& operator[](unsigned int i) { return std::vector<T>::at(i); }

	const T& operator[](unsigned int i) const { return std::vector<T>::at(i); }
};

int main()
{
    Vector<int> a { 1,2,3,4 } ; // direct initialisation
    Vector<int> b = { 1,2,3,4 } ; // copy initialisation
    if( a == b ) std::cout << "ok.\n" ;
}

http://rextester.com/KNGZ81819

It is easier with a compiller that supports inherited constructors http://www.stroustrup.com/C++11FAQ.html#inheriting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>

// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {

    typedef typename std::vector<T>::size_type size_type;

    using std::vector<T>::vector ; // inherit all constructoes

	T& operator[](unsigned int i) { return std::vector<T>::at(i); }

	const T& operator[](unsigned int i) const { return std::vector<T>::at(i); }
};

int main()
{
    Vector<int> a { 1,2,3,4 } ; // direct initialisation
    Vector<int> b = { 1,2,3,4 } ; // copy initialisation
    if( a == b ) std::cout << "ok.\n" ;
}

http://coliru.stacked-crooked.com/a/c6b0c777e43670f1
I entered the two statements into the header. It work beautifully. Thank you for your troubleshooting efforts. You got to the heart of the problem.

Now that my complier is updated and my header file is fixed, I can continue using the header file to work the rest of the problems in the book.

I skipped Chapters 12 to 16 of the book because I could not get the graphical user interface to work. I installed FLTK successfully and worked out the bugs in the header files. But, when I ran the .cpp files, I could not resolve the error messages. Please take a look at my post Window Graphics Errors. I have not received any replies so far.

Again, thank you very much. It is much appreciated.
Topic archived. No new replies allowed.