[英]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
,因此编译器尝试转换该值,但是int
和Matrix
之间没有任何转换。
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.