不能将 "Struct*" 类型的值分配给类型 "Struct*" 的实体

a value of type "Struct*" cannot be assigned to an entity of type "Struct*"

本文关键字:类型 Struct 实体 分配 不能      更新时间:2023-10-16

>我有一个返回结构类型指针的函数。我想要的是pFacialFeatures指向与返回的指针相同的地址。

struct Features
{
    CvRect* face_;
    CvRect* nose_;
    CvRect* eyesPair_;
    CvRect* rightEye_;
    CvRect* leftEye_;
    CvRect* mouth_;
};
Features* Detect()
{
    Features* facialFeatures = (Features*) malloc(sizeof(Features));
    return facialFeatures;
}
int main(int argc, char* argv[])
{
    Features* pFacialFeatures;
    pFacialFeatures = Detect();
}

它给了我错误:

智能感知:无法将"功能 *"类型的值分配给类型为"功能 *"的实体

注意:也许您可能认为这个问题与这个问题相同。在这个问题中,声明结构存在问题。我真实地宣布了结构。

您以某种方式通知Visual Studio这是一个C源文件而不是C++源文件 - 也许通过将文件命名为"something.c"或将其放在头文件中,然后从".h"文件包含它,或者通过悬挂"extern C"或以某种方式将文件或项目的属性设置为"编译为C"。如果你使用的是Linux/MacOS,你可能已经通过使用C编译器而不是C++编译器来完成,例如,通过键入"gcc foo.cpp"而不是"g++ foo.cpp。

结构声明的 C 语言语法与 C++ 中的语法不同。

C++声明

struct Foo {}; // C++

在 C 中等效于此:

typename struct tagFoo {} Foo; // C

因此,以下代码可以在C++中工作,但在 C 中失败:

struct Foo {};
Foo* f = (Foo*)malloc(sizeof(Foo));

更改此内容以检查C++的快速方法是替换:

Features* facialFeatures = (Features*) malloc(sizeof(Features));

Features* facialFeatures = new Features;

如果在 C 模式下编译,则会收到有关 new 的编译器错误。它是C++中的关键字,但在 C 中不是。

用 C 写行的方式是

struct Features* facialFeatures = malloc(sizeof* facialFeatures);
我相信

你需要在类型的声明之前放置结构:

struct Features* facialFeatures = (struct Features *)malloc(sizeof(struct Features));