为什么我在“形成对引用类型的引用”中出现错误地图? [英] Why do I get an error in "forming reference to reference type" map?
问题描述
如果我需要使用引用,我的替代方法是什么,我传递的数据不能改变类型,因此我不能真正地存储一个指针吗?
代码:
#include< map&
#include< iostream>
#include< string>
使用namespace std;
int main()
{
string test;
pair< string,string> p = pair< string,string>(Foo,Bar);
map< pair< string,string>,string&> m;
m [make_pair(aa,bb)] = test;
return 0;
}
错误:
< >
$ g ++ MapPair.cpp
/usr/include/c++/3.2.3/bits/stl_map.h:
在实例化
std :: map< std :: pair< std :: string,
std :: string&'MapPair.cpp:在
std :: string>,std :: string&
std :: less< std :: pair< std :: string ,
std :: string> >,
std :: allocator< std :: pair< const
std :: pair< std :: string,std :: string> ;,
std :: string& > >':MapPair.cpp:15:
从此处实例化
/usr/include/c++/3.2.3/bits/stl_map.h:221:
形成参考引用类型
函数 int main()':MapPair.cpp: 16:
std :: map,std :: string&
不匹配
std :: less>,
std :: allocator,
std :: string&>>>& [std :: pair]'operator
/usr/include/c++/3.2.3/bits/stl_pair.h:
在全局范围:
/usr/include/c++/3.2。 3 / bits / stl_pair.h:
在实例化 std :: pair< const
std :: _ Rb_tree_node
std :: pair< std :: string,std :: string> ;,
std :: string&>':
/usr/include/c++/3.2.3/bits/stl_tree.h:122:
从
实例化
我做错了什么导致这个错误?
您无法存储引用。引用仅为别名到另一个变量。
地图需要存储的字符串的副本:
map< pair< string,string> ;, string& m;
得到这个特定错误的原因是因为map中的某处, mapped_type
在您的情况下是 string&
。其中一个操作(例如 operator []
)将返回对 mapped_type
:
mapped_type&运算符[](const key_type&)
mapped_type
,将是:
string&&运算符[](const key_type& _Keyval)
您不能引用引用: p>
标准8.3.4:
没有引用引用,的引用,没有指向引用的指针。
在旁注中,我建议您使用 typedef
,因此您的代码更容易阅读:
int main()
{
typedef pair< string,string> StringPair;
typedef map< StringPair,string> StringPairMap;
string test;
StringPale p(Foo,Bar);
StringPairMap m;
m [make_pair(aa,bb)] = test;
return 0;
}
What is the alternative if I need to use a reference, and the data I am passing I cannot change the type of, hence I cannot really store a pointer to it?
Code:
#include <map>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string test;
pair<string, string> p=pair<string, string>("Foo","Bar");
map<pair<string, string>, string&> m;
m[make_pair("aa","bb")]=test;
return 0;
}
Error:
$ g++ MapPair.cpp /usr/include/c++/3.2.3/bits/stl_map.h: In instantiation of
std::map<std::pair<std::string, std::string>, std::string&, std::less<std::pair<std::string, std::string> >, std::allocator<std::pair<const std::pair<std::string, std::string>, std::string&> > >': MapPair.cpp:15:
std::string&' MapPair.cpp: In function
instantiated from here /usr/include/c++/3.2.3/bits/stl_map.h:221: forming reference to reference type
int main()': MapPair.cpp:16: no match for
std::map, std::string&, std::less >,
std::allocator,
std::string&> > >& [std::pair]' operator /usr/include/c++/3.2.3/bits/stl_pair.h: At global scope: /usr/include/c++/3.2.3/bits/stl_pair.h: In instantiation ofstd::pair<const std::pair<std::string, std::string>, std::string&>': /usr/include/c++/3.2.3/bits/stl_tree.h:122: instantiated from
std::_Rb_tree_node
What am I doing wrong to cause this errror?
You cannot store references. References are just aliases to another variable.
The map needs a copy of the string to store:
map<pair<string, string>, string> m;
The reason you are getting that particular error is because somewhere in map, it's going to do an operation on the mapped_type
which in your case is string&
. One of those operations (like in operator[]
, for example) will return a reference to the mapped_type
:
mapped_type& operator[](const key_type&)
Which, with your mapped_type
, would be:
string&& operator[](const key_type& _Keyval)
And you cannot have a reference to a reference:
Standard 8.3.4:
There shall be no references to references, no arrays of references, and no pointers to references.
On a side note, I would recommend you use typedef
's so your code is easier to read:
int main()
{
typedef pair<string, string> StringPair;
typedef map<StringPair, string> StringPairMap;
string test;
StringPair p("Foo","Bar");
StringPairMap m;
m[make_pair("aa","bb")] = test;
return 0;
}
这篇关于为什么我在“形成对引用类型的引用”中出现错误地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!