views:

109

answers:

4

I am busy with an assignment an I am getting an error from the compiler. I've got 4 yrs C# experience nil in C++.

I'm getting the error "Invalid conversion from nodeType, initializing argument 1 of void linkedListType::deleteNode(const Type&) [with Type = int]' " on the line deleteNode(current->link); of this function:

template<class Type>
void linkedListType<Type>::deleteNodePosition(int position)
{
    nodeType<Type> *current; //pointer to traverse the list
    nodeType<Type> *trailCurrent; //pointer just before current

    if(first == NULL)    //Case 1; list is empty. 
        cerr<<"Can not delete from an empty list.\n";
    else
    {
        current = first;
        int counter = 0;

        while(current != NULL && counter <= position)
            {               
                trailCurrent = current;
                current = current->link;
                counter++;
            } // end while
        deleteNode(current->link);
    }

deleteNode is defined as :

template<class Type>
void linkedListType<Type>::deleteNode(const Type& deleteItem)
{
    .....

and nodeType is defined as:

struct nodeType
{
    Type info;
    nodeType<Type> *link;
};

and I am calling the offending function with this:

linkedListType<int> llist;
llist.insertFirst(99);
llist.insertLast(94);
llist.deleteNodePosition(2);

Please help?

A: 

deleteNode takes a const Type&. You are passing it a nodeType*.

Paul Nathan
+4  A: 

Well, the problem is pretty straightforward. This:

current->link

is defined to have type

nodeType<Type>*

But this function that you are trying to pass it to

void linkedListType<Type>::deleteNode(const Type& deleteItem)

accepts an argument of type

const Type&

Since nodeType<Type>* isn't at all the same thing as const Type&, that function can't accept that parameter.

Without seeing the rest of your code it's hard to infer what you meant to do, but I would think that based on its name, the deleteNode function should accept a node (i.e. a nodeType<Type>*) rather than a piece of data contained by a node.

Tyler McHenry
Thanks. I ended up moving the body of deleteNode into deleteNodePostion.
callisto
A: 

current->link is of type nodeType<Type> *, but deleteNode is expecting an argument of type Type &.

Given the name (i.e. you're deleting a node) I would imagine the signature of deleteNode needs to change.

Peter Milley
+1  A: 

Your deletenode method should have the following signature:

template<class Type>
void linkedListType<Type>::deleteNode(const nodeType<Type>& deleteItem)

Also, there is a problem with your while loop. current could be NULL in which case calling deletenode will be disastrous.

naivnomore