简体   繁体   English

如何正确定义和使用返回类型为用户定义结构的函数? (在C中)

[英]How do I properly define and use a function whose return type is a user defined structure? (In C)

I did my best to search for help on this but given the keywords for this problem, I kept getting basic tutorials that didn't have my specific problem. 我尽力寻找有关此问题的帮助,但是给了这个问题的关键词,我不断得到没有特定问题的基础教程。

I was given a C library to use that defined "Matrix" as a structure: 我得到了一个C库,可以使用定义为“ Matrix”的结构:

typedef struct 
{
int height, width;
double **map;
} Matrix;

I am writing a function that needs to output a matrix. 我正在编写一个需要输出矩阵的函数。 My code currently looks like: 我的代码当前如下所示:

Matrix convolve(Matrix data, Matrix filter) //this is line 28 btw
{
    Matrix out;
    <code>
    return out
}

So far everything compiles. 到目前为止,所有内容都可以编译。 Then I try to use it in my main() block: 然后,我尝试在main()块中使用它:

int main()
{
    double a1[4][5] = <a ton of numbers>
    double f[3][3] = <a ton of other numbers>

    Matrix m1 = createMatrixFromArray(&a1[0][0],4,5); //from given C library
    Matrix mf = createMatrixFromArray(&f[0][0],3,3);

    //This is the line that doesn't compile, which is line 14:
    Matrix m2 = convolve(m1, mf);
}

Then Developer Command Prompt for VS 2017 says: 然后VS 2017的开发人员命令提示符说:

(14) error C2440: 'initializing': cannot convert from 'int' to Matrix'
(28) error C2371: 'convolve': redefinition; different basic types

What am I doing wrong? 我究竟做错了什么? Thank you in advance for your help. 预先感谢您的帮助。

It's difficult to be sure because you didn't post your complete code , but the combination of these two error messages strongly suggests that the compiler saw a use of the convolve function before it was declared. 很难确定,因为您没有发布完整的代码 ,但是这两个错误消息的结合强烈表明编译器在声明convolve函数之前就已经看到了它的用法。 For historical reasons, when a compiler sees a function that it doesn't know about, it assumes that this function returns int , rather than complaining and aborting the compilation. 由于历史原因,当编译器看到它不知道的函数时,它会假定该函数返回int ,而不是抱怨并中止编译。 From the code you show, that first use would be line 14. Since the returned int is assigned to a Matrix , the compiler tried to convert the value, but there's no conversion between int and Matrix . 在您显示的代码中,第一个用法是第14行。由于返回的int被分配给Matrix ,因此编译器尝试转换该值,但是intMatrix之间没有任何转换。

Later, on line 28, the compiler saw the definition of the convolve function, this time returning Matrix , which is incompatible with the previous (implicit) declaration. 后来,在第28行,编译器看到了convolve函数的定义,这次返回了Matrix ,它与先前的(隐式)声明不兼容。

The solution, in your case, is to define the function before it is used. 在您的情况下,解决方案是在使用功能之前先对其进行定义。 In C, and more generally in most programming languages, you need to define (or at least declare) things before they are used. 在C语言中,更常见的是在大多数编程语言中,您需要先定义(或至少声明)事物,然后再使用它们。 The main function should always be the last one in your source file since it uses other functions but no other function uses it¹. main功能应始终是源文件中的最后一个功能,因为它使用其他功能,但没有其他功能使用¹。

If the function was defined in a different source file, you would need to declare it in a header file ( .h ), and include the header file in the .c files where the function is used. 如果功能是在其他源文件中定义的,则需要在头文件( .h )中声明它,并将头文件包含在使用该功能的.c文件中。

Any halfway decent compiler would at least explicitly warn about implicit declarations: they're permitted, but they're a bad idea. 任何中途的编译器都至少会明确警告隐式声明:允许使用隐式声明,但这是一个坏主意。 Visual Studio can do it, but I think you need to raise the warning level from the default. Visual Studio可以做到这一点,但我认为您需要将警告级别从默认级别提高。

¹ Except in highly unusual programs that call main recursively. ¹ 除非非常不寻常的程序会以递归方式调用main

暂无
暂无

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

相关问题 (C编程)如何使用另一个文件中定义的数据结构和功能? - (C Programming) How do I use a data structure and function which are defined in another file? 如何在c用户定义的函数中返回“struct”数据类型? - How do I return “struct” data types in c user defined function? 如何正确定义函数返回的结构? - How can I properly define a structure which is returned by a function? 如何使用SWIG根据C函数返回类型返回特定的异常? - How do I use SWIG to return specific exceptions based on C function return type? 如何在C中为矩阵使用相同的用户定义函数? - How can I use the same user defined function for matrix in C? 如何在 C 中定义变量“N”类型,当 N 是 C 的输入并且它也由输入函数定义时(N 将提示用户输入) - How to define a variable “N” type in C when N it is an input to C and its also defined by an input function( N will prompt the user for input) 我们如何在 C 中定义和使用函数内部的结构? - how we can define and use structure inside function in C? 如何使用Fortran接口调用包含用户定义类型的C函数 - How to use Fortran interface to call a C function which contains user defined type 如何在 C 中定义结构并为其正确分配 memory? - How do I define a struct and properly allocate memory for it in C? C:定义一个long作为返回类型函数的数组 - C: Define an array of long as return type of function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM