non-pointer type warning when initializing vector with NULLs

V

Varun Kacholia

Hi everyone,
I have the following piece of code:
vector<MyClass*> my_vector(10, NULL);

However this throws the following errors:
myfile.cc: warning: converting NULL to non-pointer type
stl_algobase.h: In function `class MyClass ** fill_n<MyClass **, int,
int>(MyClass **, int, const int &)':

While I understand the reasons for fill_n craping out (as it is not
able to get a pointer to the NULL value
passed by me), is there a way to initialize vector with NULLs? Or is it
safe to assume that vectors
will be initialized with NULLs? thanks!
 
F

Frank Schmidt

Varun Kacholia said:
Hi everyone,
I have the following piece of code:
vector<MyClass*> my_vector(10, NULL);

However this throws the following errors:
myfile.cc: warning: converting NULL to non-pointer type
stl_algobase.h: In function `class MyClass ** fill_n<MyClass **, int,
int>(MyClass **, int, const int &)':

While I understand the reasons for fill_n craping out (as it is not
able to get a pointer to the NULL value
passed by me), is there a way to initialize vector with NULLs? Or is it
safe to assume that vectors
will be initialized with NULLs? thanks!

I would assume the NULL :S if you are not naive like me you can try a
vector<MyClass*> my_vector(10, (MyClass*)NULL);
 
I

Ian Collins

Varun said:
Hi everyone,
I have the following piece of code:
vector<MyClass*> my_vector(10, NULL);

However this throws the following errors:
myfile.cc: warning: converting NULL to non-pointer type
stl_algobase.h: In function `class MyClass ** fill_n<MyClass **, int,
int>(MyClass **, int, const int &)':

While I understand the reasons for fill_n craping out (as it is not
able to get a pointer to the NULL value
passed by me), is there a way to initialize vector with NULLs? Or is it
safe to assume that vectors
will be initialized with NULLs? thanks!
Why do you want to? I assume you will fill the vector, so why
initialise it? If you do, drop the NULL, the second parameter defaults
to T().
 
B

BobR

Frank Schmidt wrote in message ...
I would assume the NULL :S if you are not naive like me you can try a
vector<MyClass*> my_vector(10, (MyClass*)NULL);

What do you think NULL is? Have you looked up it's definition in your
implementation?
[ from my MinGW ]
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void*)0)
#endif
#endif

Try:
std::vector<MyClass*> my_vector( 10, static_cast<MyClass*>(0) );
// This is a C++ group, why C style cast? <G>

or:
MyClass *tmp(0);
std::vector<MyClass*> my_vector( 10, tmp );

or simply:
std::vector<MyClass*> my_vector( 10 );
std::cout<<"Temp Test my_vector.at(0)= "<<my_vector.at(0)<<std::endl;
// output: Temp Test my_vector.at(0)= 0

I'll let one of the pros 'splain it. <G>
 
F

Frank Schmidt

BobR said:
Frank Schmidt wrote in message ...

What do you think NULL is? Have you looked up it's definition in your
implementation?

No, but i saw that NULL does not match the vector type and that therefore
the compiler is choosing a different constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top