类型为“const [structname] *”的参数与类型为“const [structname] *”的参数不兼容
Argument of type “const [structname] *” is incompatible with parameter of type “const [structname] *”
我正在尝试在C++的库函数中包含结构。
结构是这样的:
struct NET_IN_OPERATE_FACERECONGNITIONDB{
DWORD dwSize;
EM_OPERATE_FACERECONGNITIONDB_TYPE emOperateType;
FACERECOGNITION_PERSON_INFO stPersonInfo;
char *pBuffer;
int nBufferLen;
}; `
包含它的函数是这样的:
BOOL CLIENT_OperateFaceRecognitionDB(
LLONG lLoginID,
const NET_IN_OPERATE_FACERECONGNITIONDB *pstInParam,
NET_OUT_OPERATE_FACERECONGNITIONDB *pstOutParam,
int nWaitTime = 1000,
);
我通过编写以下行来定义指针的结构:
const struct NET_OUT_OPERATE_FACERECONGNITIONDB{
DWORD dwSize = sizeof(NET_IN_OPERATE_FACERECONGNITIONDB);
EM_OPERATE_FACERECONGNITIONDB_TYPE emOperateType = NET_FACERECONGNITIONDB_ADD;
FACERECOGNITION_PERSON_INFO FACERECOGNITION_PERSON_INFO1;
char *pBuffer = ' ';
int nBufferLen = 10;
} *pstInParam;
但是当我使用这一行将该结构调用到函数中时:
CLIENT_OperateFaceRecognitionDB(m_LoginID, pstInParam, pstOutParam, 1000);
我收到一条错误消息,说
类型为"const NET_IN_OPERATE_FACERECONGNITIONDB *"的参数与类型为"const NET_IN_OPERATE_FACERECONGNITIONDB *"的参数不兼容
这是非常不寻常的,因为参数和参数的类型相同。我犯了什么错误?
这是非常不寻常的,因为参数和参数的类型相同。
它们不属于同一类型(错误消息告诉您,尽管这样做不是很有帮助(。
这里发生的事情是,你的pstInParam
声明实际上是在定义一个匿名struct
,然后引入一个变量,该变量是指向这个匿名struct
的指针。
简化的示例中更容易看到,这在最近的 GCC 版本上会产生更有用的错误:
struct some_struct {
int member;
};
bool some_function(const some_struct*) {
return false;
}
int main()
{
const struct some_struct {
int member = 0;
} *param;
some_function(param);
}
prog.cc: In function 'int main()':
prog.cc:15:22: error: cannot convert 'const main()::some_struct*' to 'const some_struct*' for argument '1' to 'bool some_function(const some_struct*)'
some_function(param)
您(可能,取决于CLIENT_OperateFaceRecognitionDB
的所有权语义(想要做的是声明一个some_struct
并将其地址传递给函数:
int main()
{
const some_struct param{0};
some_function(¶m);
}
。或者将其分配给免费商店:
int main()
{
const some_struct* param = new some_struct{0};
some_function(param);
delete param;
}
在后一种情况下,如果可能,请考虑使用智能指针。
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型