mardi 26 mars 2019

How do i solve overloaded function with no contextual type information error?

I'm doing a program about doubly linked list. I have function "find" that helps locate, if per se, no. 7 is anywhere within that list. This function works fine and returns pointer to that node. Then i have function "afterElement" that inserts for example no. 3 after no. 7, So it uses pointer to "find" function as parameter. I think this is where the problem stems from, but I might be wrong, you be the judge. I wanna know, how can I correctly use this function? Is there something wrong with how I pass parameters or else? The error I get is "overloaded function with no contextual type information". Here is the relevant code:

#include <iostream>
using namespace std;

struct node {
int data;
node* prev;
node* next;
};

node* find(int,node*&);
void afterElement(int,int,node*&,node*&,node* (*find)(int, node*&));

int main() {
    node* head = NULL;
    node* tail = NULL;
    // The program itself has a menu that allows for input of value in list but
    // for the sake of relevancy and shortness of code I dropped it out from here

    int x, y;
    cout << "Insert 2 values: value you wish to insert, and value you wish to insert it after. ";
    cin >> x;
    cin >> y;
    afterElement(x,y,head,tail,(*find)(y,head)); // here is the error "overloaded function..."
    return 0;
}

node* find(int x,node*& head) {
    node* curr = head;
    while ((curr != NULL) && (curr->data != x))
        curr = curr->next;
    return curr;
}

void afterElement(int x,int after,node*& head,node*& tail,node* (*find)(int x, node*& head)) {
node* N;
node* compared = (*find)(after,head);
N->data = x;
if (compared == NULL)
    cout << "There is no element " << after << " in the list!\n";
else {
    if (compared->next == NULL) {
        compared->next = N;
        N->prev = compared;
        N->next = NULL;
        tail = N;
    } else {
        compared->next->prev = N;
        N->next = compared->next;
        compared->next = N;
        N->prev = compared;
    }
}

}

Aucun commentaire:

Enregistrer un commentaire