Searching a List

I don't even know which question to ask first, so I'll just post my code and the errors that I am receiving:

int main()
1
2
3
D_Node<int> search_ptr(0, NULL, NULL);
int target = 3; //establishes insertion point
search_ptr = dlist_search(head_ptr, const Item &target);


1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class Item>
D_Node dlist_search(D_Node<Item> *head_ptr, const Item &target)
{
  D_Node<Item> *cursor;

  for (cursor = head_ptr; cursor != NULL; cursor = cursor->fore())
  {
     if (target == cursor->data_field())
     {
         return cursor;
     }
  }
  return NULL;
}


and here are the errors:

expected constructor, destructor, or type conversion before 'dlist_search'|


in function int main() expected constructor, destructor, or type conversion before 'dlist_search'|


'dlist_search' was not declared in this scope|


Can anyone help me clean this up? I don't know what to do here.

Thanks.
Shouldn't the return param be D_Node<Item>* ?

Has D_Node been defined before dlist_search ?

And the code in main looks inconsistent and incomplete.
Thanks. Didn't realize that I needed <Item> as the return type. That solved much of the problem.

I should have mentioned that I am only posting a snippet from main(). That's why it looks weird. If there is more that you need, let me know.

Now, I have this error:
expected primary expression before 'const'


I've had this before, and I don't know how to handle it. Can anyone help me?

Thanks.
Last edited on
search_ptr = dlist_search(head_ptr, const Item &target);

You need to replace "const Item& target" with an actual variable.
Last edited on
Thank you. I tried this. No matter what I do, a different error pops out. I am playing Whack-a-Mole.

After changing it to a variable (3 in this case), it says no matching function call. I changed the prototype to "...Item &target);

Please, somebody, tell me what to do. How do I call this crazy function correctly. I need to know the following:

a) what is the correct function prototype
b) how to call it

I've looked everywhere, read everything I can, and I am still stuck. I'm begging. Thanks.
Just shoot me.
To call the function, you'd need to provide it an Item variable

eg

1
2
3
4
Item myItem;
// whatever you need to do with myItem
// initialize search_ptr and head_ptr, as well.
search_ptr = dlist_search(head_ptr, myItem);


The function dlist_search is looking for a constant reference to an Item object. Make sure head_ptr is a D_Node<Item>* object.

If more errors occur, it would help to see more code.
Let me post this all since I've tried so many things that I don't even know which way is up.

First, what I am trying to do is to insert a new node in a double-linked list. In order to do that, I need to identify the node BEFORE the spot where I want to insert the new node. My list up to this point consists of integers {1,2,3,5,6}. I want to insert a {4} in the appropriate spot.

So, for now, I am focusing on returning that node (where the 3 is).

Here is the entirety of main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    //create a new list (single node)
    D_Node<int> first(3, NULL, NULL);
    D_Node<int> *head_ptr;
    D_Node<int> *tail_ptr;

    head_ptr = &first;
    tail_ptr = &first;

    //insert two new nodes at the end of a list
    dlist_tail_insert(tail_ptr, 5);
    dlist_tail_insert(tail_ptr, 6);

    //insert two new nodes at the head of the list
    dlist_head_insert(head_ptr, tail_ptr, 2);
    dlist_head_insert(head_ptr, tail_ptr, 1);

    //search the list for the node with 3 as its data_field
    D_Node<int> previous_ptr(0, NULL, NULL);
    previous_ptr->set_fore(dlist_search(head_ptr, 3));
}


And my dlist_search function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class Item>
D_Node<Item> dlist_search(D_Node<Item> *head_ptr, Item &target)
{
        D_Node<Item> *cursor;

        for (cursor = head_ptr; cursor != NULL; cursor = cursor->fore())
		{
            if (target == cursor->data_field())
            {
                return cursor;
            }
        }
        return NULL;
}


I'm getting a no matching function call on the dlist_search. I can tell that this logic isn't right, but I can't see how to change it. My ultimate goal is to capture this search result in the node called previous_ptr.

Thanks for helping. I'm banging my head against a wall.
Couldn't you just do previous_ptr = dlist_search(...);?

Make sure all your template functions are in the .h(pp) file, NOT the .cpp. Template functions are special and need to be fully defined in the header file.
Thanks. I have the files set up correctly. There are quite a few other functions, many of which are working correctly, so that part should be OK.

I tried your idea, but I still get a "no matching function call" error. The problem that won't go away is that I don't know how to call dlist_search. Once I figure that out, I may (probably will) find that I am not capturing it correctly, but one domino at a time...

Thanks for your advice. This is killing me.
If you copy-pasted that, make sure you change dlist_search's return type to D_Node<Item>* (as mentioned before)
Here comes a shocker: latest result: "no matching function call.." This is spy code for "find a brick wall and smash your head into it 1000 times.
Topic archived. No new replies allowed.