2

I'm pretty new to C++ and even looking to related Q/A on SO I didn't find a proper answer to my issue.

I'm really struggling trying to instantiate a new map of pointers map<p1*, p2*>.

So far, I tried:

std::map<myType*, myType*> mapOfVertices = new std::map<myType, myType>;

Or:

std::map<myType*, myType*> mapOfVertices = new std::map<myType*, myType*>;

But I'm always getting:

error: conversion from ‘std::map*’ to non-scalar type ‘std::map’ requested std::map mapOfVertices = new std::map;

I'm very confused since I already managed to initialize successfully other objects like:

myType* myObj = new myType;

I'm sorry if my question is a bit trivial. Thanks in advance.

AndreaM16
  • 3,917
  • 4
  • 35
  • 74
  • note that you should prefer to *not* use raw pointers to manage memory where you can help it, and that tossing `new` about willy-nilly is a good way to get memory leaks. – jaggedSpire Nov 10 '16 at 19:26
  • In C++ you very rarely need to use `new`, `std::map mapOfVertices;` is enough. Consult a beginners book, though. – krzaq Nov 10 '16 at 19:27
  • 1
    Do you really want to use a pointer as the key in a map? Remember that by default, the `map` will compare the contents of pointers, *not the item they are pointed to*. If you want to compare the target of the key pointer, you will need a custom comparator. – Thomas Matthews Nov 10 '16 at 19:27
  • C++ is not a low-level language like Java. If you want a map, you just define a map, and the compiler handles the memory allocation automatically; you don't have to use `new` to tell it to do the allocation. You probably shouldn't be using pointers either. You probably want `std::map mapOfVertices;` – Jerry Coffin Nov 10 '16 at 19:27
  • *I'm pretty new to C++* -- Given that information, what are you trying to accomplish by using a map of pointers to pointers? – PaulMcKenzie Nov 10 '16 at 19:28
  • I'm forced to use a map to those pointers. I'm working on an university project and the teacher gave us this type to use and I can access it only through pointers @PaulMcKenzie. Thanks for the information everybody. – AndreaM16 Nov 10 '16 at 19:29
  • Prefer not to use pointers, as they will complicate the program tremendously. Stick with instantiations and variables instead. – Thomas Matthews Nov 10 '16 at 19:29
  • @AndreaM16 -- With all due respect, that didn't really answer my question. What are you trying to accomplish with a map of pointers to pointers? Even if it's a "university project", at least get some **better** information, even if it's in a comment or two, on how to actually accomplish the goal *in the real world of C++*. – PaulMcKenzie Nov 10 '16 at 19:30
  • 6
    @AndreaM16: In my experience, it generally works out better if the person teaching a course in a subject isn't completely clueless about the subject matter. – Jerry Coffin Nov 10 '16 at 19:31
  • @PaulMcKenzie I need to build a `map` where vertex is like `Dcel::Vertex*`. I can't use any vertex if I don't access them through `Dcel::Vertex*`. – AndreaM16 Nov 10 '16 at 19:34
  • I know @JerryCoffin but, unluckily I have to work with what they gave me. – AndreaM16 Nov 10 '16 at 19:35
  • @AndreaM16: Is this a required course, related directly to your major course of study? If so, I'd honestly think about transferring to a different university. If not, I'd think about dropping the class and taking one that's taught competently instead. And yes, I realize you weren't asking about what university to attend... – Jerry Coffin Nov 10 '16 at 19:39
  • @AndreaM16 Well here is just one of the myriad of problems with this -- When you create a map, it is ordered by key. You have absolutely no control over where those pointers as keys wind up in the map's sorting order unless your map has a custom sort for a Dcel::Vertex is. If your program relies on the order of the keys being consistent between runs, your program will more than likely not behave the same way each run. So you wind up with a program that basically gives random results between runs. Using pointers as keys (not necessarily data, but that's bad enough), is not a good idea. – PaulMcKenzie Nov 10 '16 at 19:39
  • @JerryCoffin I think that's a bit off-topic and also escalated quickly. @PaulMcKenzie No, I don't need to rely on the order of the keys in this particular task. I will switch to `unordered_map` or `lib bopost unordered_map` as soon as I'll be done with debugging. – AndreaM16 Nov 10 '16 at 19:42
  • 1
    Once had to take a course in databases taught by an instructor with a PhD in food sciences. It was really, really bad. – user4581301 Nov 10 '16 at 19:48

1 Answers1

6

You don't need to allocate the map object itself on the heap, you can just create it on the stack.

std::map<myType*, myType*> mapOfVertices;

That code sufficiently creates the map. If, for whatever reason, you did want a pointer to the map (which involves allocating it on the heap), you'd need to change the declaration to

std::map<myType*, myType*> * mapOfVertices = new std::map<myType*, myType*>;

(Note the extra star between the type and the variable name)

Based on the code you've provided, and by your own admission, you are indeed a novice to C++, as most experienced C++ programmers know to only work with raw pointers when absolutely necessary, and your use case definitely does not represent such a case. I strongly, strongly recommend you pick up a book. There's a great listing here with suggestions.

Community
  • 1
  • 1
Xirema
  • 19,889
  • 4
  • 32
  • 68
  • Hi, thanks for the answer and the clarifications. Unluckily, I can access this particular type only through pointers, I cannot decide on that. – AndreaM16 Nov 10 '16 at 19:36
  • @AndreaM16 Understood. The good news is, if there's no requirement that the map object itself be a pointer, then the first version of the code I provided will work for you. It will handle the `Vertex*` objects just fine. If the map itself *must* be accessed through a pointer, then use the second version. – Xirema Nov 10 '16 at 19:37
  • I'm going for the second one. It works properly. I'm having some memory alloc issues on that particular task and I'm wondering if it's caused by a bad initialization of this map in particular. – AndreaM16 Nov 10 '16 at 19:39
  • @AndreaM16 Why must the `map` itself be on the heap? Adding more `*` very rarely solves problems, it makes them worse. "I'm going for the second one" -- that is almost certainly a mistake. Everyone is telling you it is a bad idea. None of your restrictions you have mentioned make it make any sense to have a pointer to a map of pointers; a map of pointers could make a tiny bit of sense. – Yakk - Adam Nevraumont Nov 10 '16 at 19:48