简体   繁体   English

将struct var分配给函数的retval时出现“不兼容的类型”

[英]“Incompatible types” when assigning struct var to retval of function

I have a function that returns a struct (not a struct pointer), but when trying to set the return value to a struct variable of the same type, I get "incompatible types." 我有一个返回结构体(而不是结构体指针)的函数,但是当尝试将返回值设置为相同类型的结构体变量时,会出现“不兼容的类型”。

This is what the struct definition and function implementation look like: 这是结构定义和函数实现的样子:

typedef struct{
    int ssn;
    char FirstName[12];
    char LastName[12];
    int income;
} MyRecord;

MyRecord parseNextRecord()
{
    MyRecord record;
    // parse and initialize
    return record;
}

And this is me calling it from within my main function: 这是我从我的主要功能内调用它:

MyRecord nextRecord;
nextRecord = parseNextRecord(); // "error: incompatible types in assignment"

Really stumped about this. 真的为此感到难过。 Thanks in advance for your help. 在此先感谢您的帮助。

Without seeing the header file, the probable cause is that main() does not see a declaration of the parseNextRecord() function which results in the compiler generating an implicit declaration for it, with a return type of int . 没有看到头文件,可能的原因是main()没有看到parseNextRecord()函数的声明,这导致编译器为其生成隐式声明,返回类型为int This will cause the incompatible assignment error as it is not possible to assign an int to a MyRecord . 这将导致不兼容的分配错误,因为无法将int分配给MyRecord To resolve, add declaration to the header file: 要解决,请将声​​明添加到头文件中:

extern MyRecord parseNextRecord();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM