Topic: Containers of pointers


Author: "Alex Jonas" <ajonas@altavista.net>
Date: 1999/01/28
Raw View
Is this valid Standard C++? It fails on the two compilers I have access to.

----

#include <map>
using namespace std;

void f(void)
{
    const char* q;
    map<char*, int> p;
    p.find(q);  // Cannot convert from const char* to char* const &
}

----

Cheers
Alex




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Garrett Potts" <potts@maelstrom.cs.fit.edu>
Date: 1999/01/29
Raw View
Hello


Try this

#include <map>
using namespace std;
typedef map<char*, int> my_type;

void main(void)
{
    my_type     p;
    const my_type::key_type q("");
    p.find(q);

}

Take care

Garrett



Alex Jonas wrote in message <36afb335.0@newsread3.dircon.co.uk>...
>
>Is this valid Standard C++? It fails on the two compilers I have access to.
>
>----
>
>#include <map>
>using namespace std;
>
>void f(void)
>{
>    const char* q;
>    map<char*, int> p;
>    p.find(q);  // Cannot convert from const char* to char* const &
>}
>
>----


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Jeff Greif <jmg@trivida.com>
Date: 1999/01/29
Raw View
See the posting "meaning of typedef X* XP ..." in this newsgroup 9 Jan 1999,
and the explanatory replies.

If you change the declaration of q below to
char* const q;
it should work.
Jeff

Alex Jonas wrote:

> Is this valid Standard C++? It fails on the two compilers I have access to.
>
> ----
>
> #include <map>
> using namespace std;
>
> void f(void)
> {
>     const char* q;
>     map<char*, int> p;
>     p.find(q);  // Cannot convert from const char* to char* const &
> }



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/01/30
Raw View
On 28 Jan 1999 18:32:25 GMT, Alex Jonas <ajonas@altavista.net> wrote:

>Is this valid Standard C++? It fails on the two compilers I have access to.

The code is indeed ill-formed.


>void f(void)
>{
>    const char* q;
>    map<char*, int> p; // LINE2
>    p.find(q);  // Cannot convert from const char* to char* const &
>}

'q' has type "char const *" which means that
    q can   be modified ('q' can be made to point to somewhere else)
   *q can't be modified (the stuff 'q' points to can't be modified)

The generic template function is,
   std::map<Key,Data>::find(const Key&);
With Key==char* and Data==int this becomes
   std::map<char*,int>::find(char *const &);

The argument is a reference to a "char *const"
  arg  can't be modified ('arg' can't be made to point somewhere else)
  *arg can   be modified ('the stuff 'arg' points to can be modified)


The most likely solution is to change the map definition
     map<const char *, int> p; // LINE2

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Dave Gallucci" <dgall812@concentric.nose_pam.net>
Date: 1999/01/31
Raw View
Your map is defined as taking a char*, not a const char*. Secondly,
you never inserted the pointer into your map. Although fixing the
first problem will allow you to compile, the find method will never
find anything, since it has no elements. All you did was declare
an instance of type map<char*, int>.  FWIW, you might want to get
into the habit of not saying "using namespace std". You can either
say std::map<char*,int> or typedef your type at the appropriate
location.

HTH,
DG

Alex Jonas wrote in message <36afb335.0@newsread3.dircon.co.uk>...
>
>Is this valid Standard C++? It fails on the two compilers I have access to.
>
>----
>
>#include <map>
>using namespace std;
>
>void f(void)
>{
>    const char* q;
>    map<char*, int> p;
>    p.find(q);  // Cannot convert from const char* to char* const &
>}
>
>----


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]