【发布时间】:2015-03-12 02:27:54
【问题描述】:
我有以下代码给我这个错误
main.cpp(41): error C2664: 'std::pair std::make_pair(_Ty1,_Ty2)' : 无法将参数 1 从 'Handle' 转换为 'unsigned int &'
我的示例程序是
#include <vector>
#include <utility>
typedef unsigned int u32;
typedef u32 Handle;
struct File
{
File()
: ch(0),
pageIdx(0)
{
}
Handle ch : 8;
u32 pageIdx;
};
int main() {
std::vector<std::pair<Handle, u32> > toTrim;
toTrim.reserve(64);
File* m_pFirstPage = new File();
File* pRef = m_pFirstPage;
toTrim.push_back(std::make_pair(pRef->ch,pRef->pageIdx));
return 0;
}
当我尝试静态转换时,即
toTrim.push_back(std::make_pair(static_cast<unsigned int>(pRef->ch), pRef->pageIdx));
我收到以下错误
main.cpp(41): error C2664: 'std::pair std::make_pair(_Ty1,_Ty2)' : 无法将参数 1 从 'unsigned int' 转换为 'unsigned int &'
谁能帮我解决它并解释我做错了什么。
【问题讨论】:
-
我认为它不喜欢位域。如果这是你想要的,为什么不使用 8 位整数呢?还有为什么要动态分配文件对象?
-
删除了我的答案,因为我认为这可能是编译器问题,您的
static_cast版本在 GCC 5 上对我来说可以正常工作,您使用的是什么编译器? -
对于错误符号“C2664”我会说 VC++
-
我在 Microsoft Visual Studio Professional 2013 上运行代码
-
你需要“:8”符号吗?没有它,它可以正确编译