需要帮助解决错误 C2664
Require Help Resolving error C2664
我有以下代码给我这个错误
main.cpp(41): 错误 C2664:"std::p air std::make_pair(_Ty1,_Ty2)":无法将参数 1 从"句柄"转换为"无符号 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): 错误 C2664:"std::p air std::make_pair(_Ty1,_Ty2)":无法将参数 1 从"无符号 int"转换为"无符号 int &"
有人可以帮我解决它并解释我做错了什么。
正在发生的事情是,您正在使用: 8
表示法指定位字段。
更多信息请见: http://www.tutorialspoint.com/cprogramming/c_bit_fields.htm
这会在句柄变量上创建一个 8 位的伪字符字段,而不是typedef u32 Handle
定义的 32 位。 std::make_pair
需要通过引用传递其参数。
由于Handle ch : 8
的类型与Handle
不同,因此不能通过引用传递它,因为被视为未定义的行为,通过引用传递的变量。
更多信息请见: 如何强制转换变量成员以将其作为函数的引用参数传递
如果您需要: 8
字段,您可以使用额外的变量来正确创建对。
#include <vector>
#include <utility>
typedef unsigned int u32;
typedef u32 Handle;
struct File
{
File()
: ch(0),
pageIdx(0)
{
}
Handle ch : 8; //Different type than just Handle
u32 pageIdx;
};
int main() {
std::vector<std::pair<Handle, u32> > toTrim;
toTrim.reserve(64);
File* m_pFirstPage = new File();
File* pRef = m_pFirstPage;
unsigned int ch_tmp = pRef->ch; //<-Extra variable here
toTrim.push_back(std::make_pair(ch_tmp, pRef->pageIdx));
return 0;
}
相关文章:
- 使用解算器SparseLU并获得错误C2664
- VS 2017 错误 C2664 地图插入尝试
- 使用自定义对象声明shared_ptr数组时出现错误 C2664
- 编译器错误 C2664 使用 std::map 和自定义类作为其值
- 如何在 VC++ 中通过引用传递另一个对象的方法(错误 C2664)
- 在C++中构造模板函数时出现编译错误 C2664 和 C2440
- 了解错误 C2664:无法转换参数 1
- 错误 C2664:无法从'NvPhysicalGpuHandle'转换参数 1
- 错误 C2664 和 E0167 被难倒
- 尝试创建unique_ptr时出现错误 C2664
- 错误 C2664:"errno_t wcstombs_s(size_t *,字符 *,size_t,常量 wchar_t *,size_t)":无法转换参数 4
- 错误C2664:无法转换参数错误
- 在集合中转换为小写 C++ 错误 C2664
- 错误 C2664:无法将参数 1 从 'int' 转换为 'int []'
- 将互斥锁数组传递给函数时出现错误 C2664
- 错误 C2664:"发送":无法将参数 2 从"服务器问候语"转换为"常量字符 *"
- 错误C2664:无法转换参数(指纹传感器检测)
- C++ 错误 1 错误 C2664 传递数组指针
- 错误 C2664:'CComboBox::InsertString':无法将参数 2 从 'const char [4]' 转换为 'LPCTSTR'
- 创建面向对象的链表时出现编译问题(编译器错误C2664)