Search code examples
c++undefined-referenceexitstatus

My DEV C++ IDE continously gives me the message:"Id returned 1 exit status" and "Undefined reference to "function(int, int)". How to fix it?


Here's my code

#include <stdio.h>
#include <conio.h>

int func(int , int);
main(){
int m[3][3]={(0,0,0),
             (0,0,0),
             (0,0,1)};
int n=3,a;
a=func(m[3][3], n);
if(a==1) printf("True");
else printf ("False");
getch();
}

int func(int m[3][3], int n)
{
int i, j, k=0;
for (i=0;i<n;i++)
    for (j=0;j<n;j++)
        if(m[i][j]==1) k=1;

    return k;

}

Where am I mistaken? The message of the IDE is : Funk.cpp:(.text+0x4b): undefined reference to `func(int, int)' [Error] ld returned 1 exit status


Solution

  • The function prototype and definition of func doesn't match. Hence the error. Fix it by changing the function prototype to

    int func(int[3][3], int);
    

    The next mistake is that this:

    a=func(m[3][3], n);
    

    should be changed to

    a=func(m, n);
    

    as you want to pass the array, instead of an invalid memory location beyond the array.


    And I think that you wanted

    int m[3][3]={{0,0,0},
                 {0,0,0},
                 {0,0,1}};
    

    instead of

    int m[3][3]={(0,0,0),
                 (0,0,0),
                 (0,0,1)};
    

    Also, it is better to use a standard definition of main, i.e, change

    main(){
    

    to

    int main(void) {