IAE*_*IAE 4 c++ return function map
相当直截了当的问题.我有一个地图,我希望通过调用这样的函数来初始化:
map<string, int> myMap;
myMap = initMap( &myMap );
map<string, int> initMap( map<string, int> *theMap )
{
/* do stuff... */
Run Code Online (Sandbox Code Playgroud)
然而,编译器正在呻吟.这是什么解决方案?
编辑1:
对不起,我搞砸了.代码写得正确*theMap,但是当我发布问题时,我没有注意到我省略了*.所以为了回答评论,我得到的错误信息是:
1>Roman_Numerals.cpp(21): error C2143: syntax error : missing ';' before '<'
被抛出的
map<char, int> initMap( map<char, int> *numerals );
在定义函数时,使用VC++ 2010 Express并再次出现相同的错误.
jil*_*wit 14
要么:
map<string, int> myMap;
initMap( myMap );
void initMap( map<string, int>& theMap )
{
/* do stuff in theMap */
}
Run Code Online (Sandbox Code Playgroud)
或做:
map<string, int> myMap;
myMap = initMap( );
map<string, int> initMap()
{
map<string, int> theMap;
/* do stuff in theMap */
return theMap;
}
Run Code Online (Sandbox Code Playgroud)
即让函数初始化你给它的地图,或者获取函数给你的地图.你们两个都做了(没有return声明!)
我会选择第一个选项.
这可能是抱怨,因为你传递了地图的地址,但是你的函数按值接受地图.
你可能想要更像这样的东西:
void initMap(map<string, int>& theMap)
{
/* do stuff...*/
}
Run Code Online (Sandbox Code Playgroud)