0

I'm studying the C++ programming language and I'm having some problem with my first vector. If i follow the example in the book (programming principles and practice using C++), this is the example :

vector<int> v = { 5, 7, 9, 4, 6, 8 }; 

and here what my compiler says : Errore 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'Vector' c:\users\pierob\documents\visual studio 2013\projects\learnprogramming\learnprogramming\main.cpp 7 1 learnprogramming

can you help me please ? I have visual studio 2013 express (I use the visual C++ compiler november 2013 ctp).

piero borrelli
  • 737
  • 2
  • 9
  • 20
  • I haven't used visual studio, but what I can tell you is that the above example would work only in case of c++11. Set appropriate flags while compiling. http://www.cplusplus.com/reference/vector/vector/vector/ – learningToCode Nov 28 '14 at 17:30
  • http://www.cplusplus.com/forum/beginner/149062/ - thanks for posting this question on multiple forums. This might help - v(6) because you initialize the vector and make room for 6 elements and initialize those elements to 0. Show us more code. –  Nov 28 '14 at 17:32
  • 1
    This just means that your version of VS does not fully support c++11. – juanchopanza Nov 28 '14 at 17:33
  • @learningToCode how can I set those flags ? what flags do I need to run my program ? – piero borrelli Nov 28 '14 at 17:35
  • @pieroborrelli -std=c++0x Something like this on linux: g++ -std=c++0x filename.cpp This might be relevant to you: http://stackoverflow.com/questions/5121529/how-to-enable-c0x-features-in-visual-studio-initializer-lists-support – learningToCode Nov 28 '14 at 17:38
  • 1
    could you please post all the file? In your error message I see "Vector" instead of "vector". – ebasconp Nov 28 '14 at 17:42

2 Answers2

1

The error message states Vector, not vector, so something looks suspicious as to the code you're actually compiling.

Using Visual Studio 2013 Update 3, the following compiles with 0 errors:

#include <vector>

int main()
{
    std::vector<int> v = { 5, 7, 9, 4, 6, 8 };
}

Output:

1>  main.cpp
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Please copy the above code and compile it to ensure it gives no errors.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • yes it gives me error, this is what my compile says Errore 1 error C2039: 'Vector' : is not a member of 'std....................Errore 2 error C2143: syntax error : missing ';' before '<'.........................Errore 3 error C2143: syntax error : missing ';' before '{'.........................Errore 4 error C2143: syntax error : missing ';' before '}'.......... – piero borrelli Nov 28 '14 at 19:30
  • I have tried to compile the program without using the library defined by the author to see what happens and it work properly, so I think that there is an error in the std_lib_facilities header and now I have to find how to correct that error – piero borrelli Nov 28 '14 at 19:45
0

On Line 99 (for me) of the std_lib_facilities.h header file is an apparently disgusting macro hack to get a range checked vector that says:

#define vector Vector

I changed that to:

#define vector vector

and your code:

vector<int> v = { 5, 7, 9, 4, 6, 8 };

seems to work as intended.

amhamor
  • 1
  • 1