Frage

I'm trying to understand why would the foo function trigger the linker error "undefined reference to foo" only if I place its definition last in the list, and if the declaration is anywhere else upper in the list, the error isn't there anymore. If it helps, I'm using DevC++ 5.3.0.4 to create a C project with this file as main file. All the other function work properly, and I'd like to understand why the position of the definition is so important, since this function doesn't use the other functions?

Initially, it did, but while trying to figure out why this error appeared I've eliminated what I thought to be possible causes, thus all other function calls within the foo function body.

I guess it might have something to do with the array parameter, but what is so special about this function and not about the others?

#include <stdio.h>
#include <stdlib.h>





void removeDuplicate(int position, int vector[], int n); 
int belongsToSet(int x, int v[], int vectorDimension);
int isDuplicate(int position, int vector[], int n);
void displaySet(int v[], int dimension);
int foo(int v[]);




int main(int argc, char *argv[]) {
    int m = -1, n = -1, i, j;
    int a[20], b[20], reunion[40], intersection[40], abDifference[20],
     baDifference[20];
    int reunionIndex = 0, intersectionIndex = 0, abDifferenceIndex = 0,
      baDifferenceIndex = 0;


    i = foo(a);
    getch();
    return 0;
}

int belongsToSet(int x, int v[], int vectorDimension){
    int i;
    for (i = 0; i < vectorDimension; i++){
        if (v[i] == x){
            return 1;
        }
    }
    return 0;
}



void removeDuplicate(int position, int vector[], int n){
    int i;
    for (i = position; i < n - 1; i++){
        vector[i] = vector[i + 1];
    }
}

int isDuplicate(int position, int vector[], int n){
    int i;
    for (i = 0; i < n; i++){
        if ((vector[i] == vector[position]) && (i != position)){
            return 1;
        }
    }
    return 0;
}


void displaySet(int v[], int dimension){
    printf("\n");
    if (dimension == 0){
        printf("\xed");
    }else{
        int i;
        printf("{");
        for (i = 0; i < dimension; i++){
            printf("%d, ", v[i]);
        }
        printf("\b\b}");
    }

int foo(int v[]){
    return 1;
}

}
War es hilfreich?

Lösung

foo at the bottom, as it appears in the code presented, is INSIDE the definition of displaySet.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top