C++ 为什么这一行不编译?
以下代码未编译(Ideone链接:)。为什么 编辑:根据C++ 为什么这一行不编译?,c++,stl,C++,Stl,以下代码未编译(Ideone链接:)。为什么 编辑:根据std::map,const Key&类型被std::map::find接受。因此,将const int*传递到find()应该可以 #include <iostream> #include <map> using namespace std; class ConstDemo { std::map<int*, int> m_Map; public: ConstDemo(int count
std::map
,const Key&
类型被std::map::find
接受。因此,将const int*
传递到find()
应该可以
#include <iostream>
#include <map>
using namespace std;
class ConstDemo
{
std::map<int*, int> m_Map;
public:
ConstDemo(int count, int* pArray)
{
for(int i=0; i < count; ++i)
{
m_Map.insert(std::make_pair(&(pArray[i]),0));
}
}
bool Find(const int* i) const
{
// DOESN"T COMPILE!
return (m_Map.find(i) != m_Map.end());
}
};
int main() {
int a[10];
ConstDemo cd(10, a);
if(cd.Find(&a[5]))
cout << "Found!" << std::endl;
return 0;
}
#包括
#包括
使用名称空间std;
类ConstDemo
{
std::map mu map;
公众:
ConstDemo(整数计数,整数*帕雷)
{
对于(int i=0;i coutint*const
和int*const
不一样。请尝试将其更改为int*const
:
bool Find(int* const i) const
这是因为您的key\u type
是int*
(std::map m\u map;
)和m\u map.find
需要一个const key\u type
作为参数,即在您的例子中是int*const
。但是您正在传递一个const int**
如果将int*
传递给m\u Map.find
,也可以,因为它可以将int*
转换为int*const
,但不能将int*
转换为const*
另外,在下一行intmain
的末尾缺少一个分号:
ConstDemo cd(10, a)
现在,在这里看到它
编辑:
在编辑完你的问题之后
根据std::map
的文档,std::map::find
接受const Key&
类型。因此将const int*
传递到find()
应该可以
#include <iostream>
#include <map>
using namespace std;
class ConstDemo
{
std::map<int*, int> m_Map;
public:
ConstDemo(int count, int* pArray)
{
for(int i=0; i < count; ++i)
{
m_Map.insert(std::make_pair(&(pArray[i]),0));
}
}
bool Find(const int* i) const
{
// DOESN"T COMPILE!
return (m_Map.find(i) != m_Map.end());
}
};
int main() {
int a[10];
ConstDemo cd(10, a);
if(cd.Find(&a[5]))
cout << "Found!" << std::endl;
return 0;
}
const Key&
是一个常量Key
,因此在您的情况下,需要传递一个常量
int*
现在。但是const int*
没有定义一个常量int*
,它只定义了一个指向const int
的指针int*const
定义了一个常量int*
,所以如果你传递const int*
const*
和int*const
,它就不会出错我。尝试将其更改为int*const
:
bool Find(int* const i) const
这是因为您的key\u type
是int*
(std::map m\u map;
)和m\u map.find
需要一个const key\u type
作为参数,即在您的例子中是int*const
。但是您正在传递一个const int**
如果将int*
传递给m\u Map.find
,也可以,因为它可以将int*
转换为int*const
,但不能将int*
转换为const*
另外,在下一行intmain
的末尾缺少一个分号:
ConstDemo cd(10, a)
现在,在这里看到它
编辑:
在编辑完你的问题之后
根据std::map
的文档,std::map::find
接受const Key&
类型。因此将const int*
传递到find()
应该可以
#include <iostream>
#include <map>
using namespace std;
class ConstDemo
{
std::map<int*, int> m_Map;
public:
ConstDemo(int count, int* pArray)
{
for(int i=0; i < count; ++i)
{
m_Map.insert(std::make_pair(&(pArray[i]),0));
}
}
bool Find(const int* i) const
{
// DOESN"T COMPILE!
return (m_Map.find(i) != m_Map.end());
}
};
int main() {
int a[10];
ConstDemo cd(10, a);
if(cd.Find(&a[5]))
cout << "Found!" << std::endl;
return 0;
}
const Key&
是一个常量Key
,因此在您的情况下,需要传递一个常量
int*
现在。但是const int*
并没有定义一个常量int*
,它只是定义了一个指向const int
的指针int*const
定义了一个常量int*
,所以如果你传递const int*
,它就会给出错误。简短回答:
使用
长答覆:
在C++11之前,std::map::find
的声明:
iterator find( const Key& key );
const_iterator find( const Key& key ) const;
参数类型是常量键&
,而不是键
或键&
在您的情况下,键是int*
,而不是cons int*
如果我们在声明中替换键
,它们看起来像:
iterator find( int * const & key ); // Not "const int*&"
// That's unforunate part
const_iterator find( int * const & key ) const;
不能使用类型为const int*
的变量调用此类函数。这将允许函数中断指针指向的对象的常量。简短回答:
使用
长答覆:
在C++11之前,std::map::find
的声明:
iterator find( const Key& key );
const_iterator find( const Key& key ) const;
参数类型是常量键&
,而不是键
或键&
在您的情况下,键是int*
,而不是cons int*
如果我们在声明中替换键
,它们看起来像:
iterator find( int * const & key ); // Not "const int*&"
// That's unforunate part
const_iterator find( int * const & key ) const;
此类函数不能使用类型为const int*
的变量调用。这将允许函数中断指针指向的对象的常量。您读到这里了吗:/usr/include/c++/6/bits/stl_map.h:1079:7:注意:参数1的转换格式错误:prog.cpp:22:28:error:inv从“const int*”到“std::map::key_type{aka int*}”的alid转换映射中作为键的指针几乎总是错误的。您读到这里了吗:/usr/include/c++/6/bits/stl_map.h:1079:7:注意:参数1的转换格式错误:prog.cpp:22:28:错误:从“const int*”到“std::map::key_type{aka int*}”的转换无效“指针作为地图中的键几乎总是错误的。