non-pointer type warning when initializing vector with NULLs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Varun  Kacholia

    #1

    non-pointer type warning when initializing vector with NULLs

    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!

  • Frank Schmidt

    #2
    Re: non-pointer type warning when initializing vector with NULLs


    "Varun Kacholia" <kacholia@gmail .com> schrieb im Newsbeitrag
    news:1141261397 .070036.199930@ j33g2000cwa.goo glegroups.com.. .[color=blue]
    > 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![/color]

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


    Comment

    • BobR

      #3
      Re: non-pointer type warning when initializing vector with NULLs


      Frank Schmidt wrote in message ...[color=blue]
      >
      >"Varun Kacholia" <kacholia@gmail .com> schrieb im Newsbeitrag
      >news:114126139 7.070036.199930 @j33g2000cwa.go oglegroups.com. ..[color=green]
      >> 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![/color]
      >
      >I would assume the NULL :S if you are not naive like me you can try a
      >vector<MyClass *> my_vector(10, (MyClass*)NULL) ;[/color]

      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<MyC lass*> my_vector( 10, static_cast<MyC lass*>(0) );
      // This is a C++ group, why C style cast? <G>

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

      or simply:
      std::vector<MyC lass*> my_vector( 10 );
      std::cout<<"Tem p 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>
      --
      Bob R
      POVrookie


      Comment

      • Frank Schmidt

        #4
        Re: non-pointer type warning when initializing vector with NULLs


        "BobR" <RemoveBadBobR@ worldnet.att.ne t> schrieb im Newsbeitrag
        news:F9vNf.5342 $J02.457@bgtnsc 04-news.ops.worldn et.att.net...[color=blue]
        >
        > Frank Schmidt wrote in message ...[color=green]
        >>
        >>"Varun Kacholia" <kacholia@gmail .com> schrieb im Newsbeitrag
        >>news:11412613 97.070036.19993 0@j33g2000cwa.g ooglegroups.com ...[color=darkred]
        >>> 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![/color]
        >>
        >>I would assume the NULL :S if you are not naive like me you can try a
        >>vector<MyClas s*> my_vector(10, (MyClass*)NULL) ;[/color]
        >
        > What do you think NULL is? Have you looked up it's definition in your
        > implementation?[/color]

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


        Comment

        • Ian Collins

          #5
          Re: non-pointer type warning when initializing vector with NULLs

          Varun Kacholia wrote:[color=blue]
          > 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!
          >[/color]
          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().

          --
          Ian Collins.

          Comment

          Working...