GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Aug-2009, 14:09
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Error C2664: 'ListInt::Insert' : cannot convert parameter 1 from 'ListInt *'...


Hello all,

I have tried to see where I have gone wrong here. I cannot seem to fix this error:
.\linked.cpp(85) : error C2664: 'ListInt::Insert' : cannot convert parameter 1 from 'ListInt *' to 'ListInt::IntNode *'

Here is the code if someone be so kind to look at. Thank you:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

class ListInt
{
	struct IntNode
	{
		int number;
		IntNode *next;
	};

private:
	IntNode *head;


public:
	ListInt();
	void Insert(IntNode *head,int newItem);
	int Retrieve(IntNode *head, int dataItem);
	void display();
	~ListInt();
};

ListInt::ListInt()
{

}


void ListInt::Insert(IntNode *head, int newItem)
{
	if (head == NULL || newItem < head->number)
	{
		IntNode *tempPtr = head;
		head = new IntNode;
		head->number = newItem;
		head->next = tempPtr;
	}
	else Insert(head->next, newItem);
}


int ListInt::Retrieve(IntNode *head, int dataItem)
{

}

void ListInt::display()
{
	IntNode *temp;
	temp = head;
	cout << endl;
	if (temp == NULL)
		cout << "The list is empty!" << endl;
	else
	{
		while (temp != NULL)
		{  
              cout << temp->number << " ";
			  cout << endl;
			  temp = temp->next;
		}
	 cout << "End of list!" << endl;
       }
}



int main()
 
{
	ListInt myList;
	ListInt *head = NULL;
   
    int number;

        cout << "Enter numbers to be inserted (0 to end): " <<endl;
        cin >> number;
        while (number != 0)
        {
			myList.Insert(head, number);
            cin >> number;
        }

        cout << "The list is: " <<endl;
        myList.display();
        
        //cout << "Enter numbers to be retrieved (0 to end): " <<endl;
        //myList.retrieve();
    
    return 0;
}

  #2  
Old 22-Aug-2009, 14:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error C2440 :(


Quote:
Originally Posted by ziorus
where I have gone wrong...

Here's your definition of the LIstInt::Insert function:
CPP / C++ / C Code:
void ListInt::Insert(IntNode *head, int newItem)
{
.
.
.
}

Here's what the compiler is complaining about:
CPP / C++ / C Code:
int main()
.
.
.
    ListInt *head = NULL;
 .
.
.
            myList.Insert(head, number); // <--- This is the "bad" line
.
.
.

Here is the compiler message that you posted:
Code:
'ListInt::Insert' : cannot convert parameter 1 from 'ListInt *' to 'ListInt::IntNode *'

Here are my suggestions:
  1. Look at the two pieces of your code that I abstracted above.

  2. Look at your compiler message again.

  3. Tell us what you don't understand about that message.


Regards,

Dave
  #3  
Old 22-Aug-2009, 15:01
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Re: error C2440 :(


parameter 1 would concern the "head" part correct?

CPP / C++ / C Code:
myList.Insert(myList.head, number);
  #4  
Old 22-Aug-2009, 15:09
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: error C2440 :(


From your original message:
Quote:
Originally Posted by ziorus
CPP / C++ / C Code:
void ListInt::Insert(IntNode *head, int newItem)
Note the types of each parameter.

From function main():
Quote:
CPP / C++ / C Code:
ListInt *head = NULL;
...
myList.Insert(head, number); 
The question you need to answer for yourself is whether you are passing parameters of the correct type to the method.
  #5  
Old 22-Aug-2009, 15:19
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error C2440 :(


Quote:
Originally Posted by ziorus
parameter 1 would concern the "head" part correct?
Yes.

In this context, the word "parameters" refers to the identifiers inside the parentheses of a function definition.

In your function definition, parameter 1 has data type "pointer to ListInt::IntNode"

The word "arguments" refers to the identifiers inside the parentheses of a function invocation.

Argument number1 in the function invocation has data type "pointer to ListInt"

The data types of the arguments must be consistent with the data types of corresponding parameters. See Footnote.

In your program they data type of the argument is not the same as the data type of the corresponding parameter, and C++ will not (can not) implicitly convert a pointer to one data type to a pointer to another data type.

Therefore: no joy.


Regards,

Dave

Footnote:

Sometimes the data type of an argument can be implicitly converted to the data type of the corresponding parameter. C++ compilers try to find cases like that and silently perform the conversion. For example:

CPP / C++ / C Code:
int foo(double x) // Data type of the parameter is double precision.
{
.
.
.
}

int main()
{
    int iii;
.
.
.
    iii = 42;
.
.
.
    foo(iii); // The integer value of iii is converted to the equivalent double precision value

This is dealing with numerical values, not pointers, and the conversion works.
  #6  
Old 22-Aug-2009, 16:44
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Re: error C2440 :(


ok. I got it out of that error but now the numbers are not going into the linked list. I get this error now, which I have to break or continue the program:

An unhandled exception of type 'System.AccessViolationException' occurred in linked.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

CPP / C++ / C Code:
#include <iostream>

using namespace std;

struct IntNode
{
	int number;
	IntNode *next;
};

class ListInt
{
private:
	
	IntNode *head;


public:
	ListInt();
	void Insert(IntNode *head,int newItem);
	int Retrieve(IntNode *head, int dataItem);
	void display();
	
};

ListInt::ListInt()
{

}


void ListInt::Insert(IntNode *head, int newNumber)
{
	if (head == NULL || newNumber < head->number)
	{
		IntNode *tempPtr = head;
		head = new IntNode;
		head->number = newNumber;
		head->next = tempPtr;
	}
	else Insert(head->next, newNumber);

	/*IntNode *tempPtr = new IntNode;        
	tempPtr->number = newNumber;        
	tempPtr->next = head;        
	head = tempPtr;*/

};


int ListInt::Retrieve(IntNode *head, int dataItem)
{
return 0;
}

void ListInt::display()
{
	IntNode *p = head;        
	while (p != NULL) 
	{                
		cout << p->number << " ";                
		p = p->next;        
	}        
	cout << endl;
};



int main()
 
{
	ListInt myList;
   IntNode *head = NULL;
    int number;

        cout << "Enter numbers to be inserted (0 to end): " <<endl;
        cin >> number;
        while (number != 0)
        {
			myList.Insert(head, number);
            cin >> number;
        }

        cout << "The list is: " <<endl;
        myList.display();
        
        //cout << "Enter numbers to be retrieved (0 to end): " <<endl;
        //myList.retrieve();
    
    return 0;
}
  #7  
Old 22-Aug-2009, 18:23
ziorus ziorus is offline
Awaiting Email Confirmation
 
Join Date: Aug 2009
Posts: 11
ziorus is on a distinguished road

Re: error C2440 :(


ok.... got it going... here is the code:

CPP / C++ / C Code:
#include <iostream>

using namespace std;

struct IntNode
{
	int number;
	IntNode *next;
};

class ListInt
{
private:
	
	IntNode *head;
	IntNode *cur;

public:
	ListInt();
	void Insert(IntNode **head,int newItem);
	int Retrieve(IntNode *head, int dataItem);
	void display(IntNode *p);
	
};

ListInt::ListInt(): head(NULL)
{

}


void ListInt::Insert(IntNode **head, int newNumber)
{

	IntNode *tempPtr = new IntNode;
	tempPtr->number = newNumber;        
	tempPtr->next = *head;        
	*head = tempPtr;

};


int ListInt::Retrieve(IntNode *head, int dataItem)
{
return 0;
}

void ListInt::display(IntNode *p)
{
       
	while (p != NULL) 
	{                
		cout << p->number << " ";                
		p = p->next;        
	}        
	cout << endl;
};



int main()
 
{
	ListInt myList;
   IntNode *head = NULL;
    int number;

        cout << "Enter numbers to be inserted (0 to end): " <<endl;
        cin >> number;
        while (number != 0)
        {
			myList.Insert(&head, number);
            cin >> number;
        }

        cout << "The list is: " <<endl;
        myList.display(head);
        
        //cout << "Enter numbers to be retrieved (0 to end): " <<endl;
        //myList.retrieve();
    
    return 0;
}
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.