How to create a function displaying the return type <>?

Pretty simple question. I have a map that I want to initialize by calling a function like this:

map<string, int> myMap;

myMap = initMap( &myMap );

map<string, int> initMap( map<string, int> *theMap )
{
    /* do stuff... */

However, the compiler groans. What is the solution for this?

EDIT 1:

Sorry, but I messed up. The code was correctly written using *theMap, but when I posted the question, I didn’t notice what I omitted *. Therefore, to respond to a comment, an error message appears:

1>Roman_Numerals.cpp(21): error C2143: syntax error : missing ';' before '<'

which rushes at

map<char, int> initMap( map<char, int> *numerals );

using VC ++ 2010 Express and the same error when I define a function.

+3
source share
8 answers

Or do:

map<string, int> myMap;
initMap( myMap );

void initMap( map<string, int>& theMap )
{
    /* do stuff in theMap */
}

or do:

map<string, int> myMap;
myMap = initMap(  );

map<string, int> initMap()
{
    map<string, int> theMap;
    /* do stuff in theMap */
    return theMap;
}

. , , , . ( return!)

.

+12

, , , .

:

void initMap(map<string, int>& theMap)
{
    /* do stuff...*/
}
+8

. :

map<string, int>& initMap( map<string, int>& theMap )
...
// Call initMap
map<string, int> my_map;
initMap(my_map);
+3

std::map<std::string, int> initMap();
// ...
std::map<std::string, int> myMap = initMap();

? ? . , .

+3

void initMap (map & theMap) , ?

+1

&myMap - , theMap - .

:

myMap = initMap( &myMap ); myMap = initMap( myMap );.

map<string, int> initMap( map<string, int> theMap ) map<string, int> initMap( map<string, int> * theMap ).

+1
source

A bit late in the game, but: I would suggest from the error message that you are missing

#include <map>

at the top of your code. Thus, the compiler does not know that the map should be a template, and therefore it is confused by the angle brackets that follow.

0
source
 void initMap(map<String,int> &Map)
 {
   //Do something
 }
-1
source

Source: https://habr.com/ru/post/1748533/


All Articles