3

I encountered a compilation error when trying to initialize a vector of pointers to NULL by way of the std::vector constructor. I simplify the instruction to keep it simple:

vector<int*> v (100,NULL)

I guess it has something to do with an incompatibility between const T& value= T() (the parameter of the constructor) and the very value NULL, but I would appreciate some further explanation.

Thank you

Lazaro Arribas
  • 31
  • 1
  • 1
  • 3
  • 1
    An "execution error"? What is the error? Did you run this in the debugger to confirm it was this line that was failing? – Oliver Charlesworth Jun 09 '12 at 11:15
  • No, I didn't use a debugger (i'm kind of a newbie). Also, it's not an execution but a compilation error, i was mistaken. Here you go: ArbreN.hpp:55:3: warning: passing NULL to non-pointer argument 2 of ‘std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = int, _Tp = ArbreNari::node_arbreNari*, _Alloc = std::allocator::node_arbreNari*>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator::node_arbreNari*>]’ [-Wconversion-null] – Lazaro Arribas Jun 09 '12 at 11:18
  • 5
    By the way, if you use the one-argument constructor (`vector v(100);`, they will automatically be initialized to `NULL`. – Oliver Charlesworth Jun 09 '12 at 11:19
  • 2
    I cannot reproduce the warning on ideone ([link](http://ideone.com/jneCt)). – Sergey Kalinichenko Jun 09 '12 at 11:22
  • 1
    When asking about a particular piece of code that does not compile, make sure to include the compiler error message. Complex as they are sometimes, they usually include important information that can help determine the cause of the problem. – David Rodríguez - dribeas Jun 09 '12 at 11:29
  • If you can (i.e. if are using C++11, which you should unless you have a *very good reason* not to), use `nullptr`. –  Jun 09 '12 at 12:13
  • what compiler are you using? I cannot reproduce the problem with GCC. – juanchopanza Jun 09 '12 at 12:59
  • Thanks for the comments. If pointers are set to NULL by default then I don't see the point of doing what I intended to. Anyway, I was curious of how the language treats the NULL value compared to other 'templatizatable' types. I'm using GCC (though I don't know how to find out which version) by g++ -c main.cpp. That's where I get the error (even with the simplified instruction I wrote on the post). – Lazaro Arribas Jun 09 '12 at 14:00

2 Answers2

6

If you have the relevant C++11 support, you could use nullptr:

std::vector<int*> v(100, nullptr);

However, in your particular case, there is no need to specify a default value, so the following would suffice:

std::vector<int*> v(100);
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

NULL is likely defined as 0, so you end up with

vector<int*> v(100,0);

which tries to build a vector of ints, not of int*s.

Just skip the NULL, as that is default for pointers anyway, or cast it to the correct pointer type (int*)NULL.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • 1
    I guess it is rather trying to use the constructor overload taking two input iterators (by resolving the input iterator type to be `int`). It doesn't just magically try to create a vector of `int`s, it's still a `std::vector` constructor that is called. – Christian Rau Jun 09 '12 at 12:18
  • Well, that's my way of looking at it. :-) The main problem is that NULL is a 0, which makes the constructor call look like `vector(int,int)`. That *would* have worked for a `vector`. – Bo Persson Jun 09 '12 at 13:37