Solved

Error: C2664

Posted on 2011-03-20
7
555 Views
Last Modified: 2012-05-11
Hello Everyone,

I am having an issue with calling a function in my BuddyMemory.cpp file. the function is check_free_memory.  When I call function

allocated_list->allocateMemory(&free_list->find_value(value));

The function free_list->find_value is throwing the following  error

Error      1      error C2664: 'ListNode<Object>::ListNode(Object)' : cannot convert parameter 1 from 'ListNode<Object> *' to 'int'

the return nodePtr the issue in find_value

The find_value function is with in the LinkedList.h.
 
LinkedList.h
 
#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include<iostream>
#include<vector>
#include<string>

using namespace std;

template<class Object> class ListNode;
template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);



template<class Object>
class ListNode
{
	friend ostream &operator<<(ostream& out, const vector<Object> &node_linked_list);

public:
	
	 ListNode(Object nodeValue)
      : value(nodeValue), next(NULL),
      start_blockValue( Object() ), end_blockValue( Object() ), 
      node_linked_list( new vector<Object> )
		{}


	 ListNode(Object nodeValue, Object startValue, Object endValue)
      : value(nodeValue), next(NULL),
      start_blockValue( startValue ), end_blockValue( endValue ), 
      node_linked_list( new vector<Object> )
		{}
	

	void set_rangeNumber();
	Object get_rangeNumber();

	vector<Object> *node_linked_list;

	Object value;
	ListNode<Object> *next;
	
private:
	 Object start_blockValue;
	 Object end_blockValue;

};

template<class Object>
ostream &operator<<(ostream& out, const vector<Object> &node_linked_list)
{
	for(int i =0; i != node_linked_list.size(); i++)
		out<<node_linked_list[i];
	return out;
}


template<class Object>
void ListNode<Object>::set_rangeNumber()
{
	start_blockValue += 32;
}

template<class Object>
Object ListNode<Object>::get_rangeNumber()
{
	return start_blockValue;
}

//End of ListNode--------------------------------------------------------

template<class Object>
class LinkedList
{

private:
	ListNode<Object> *head;
	 int total_free_memory;
	 int total_used_memory;

public:
	LinkedList()
	{
		head = NULL;
		total_free_memory = 0;
		total_used_memory = 0;
	}

	~LinkedList();
	void appendNode(Object);
	void appendNode(Object, int, int);

	void insertNode(Object);
	void show_List(string);
	void deleteNode(Object);

	ListNode<Object> find_value(Object);
	void allocateMemory(ListNode<Object> *);
	
	void add_free_memory(Object);
	void add_used_memory(Object);
	void set_memory(Object);

	Object getTotal_free_memory();
	Object getTotal_used_memory();

};

template<class Object>
void LinkedList<Object>::add_free_memory(Object value)
{
	free_memory += value;
	used_memory -= value;
}

template<class Object>
void LinkedList<Object>::add_used_memory(Object value)
{
	used_memory += value;
	free_memory -= value;
}

template<class Object>
void LinkedList<Object>::set_memory(Object memory)
{
	total_free_memory = memory;

	ListNode<Object> *nodePtr;

	nodePtr = head;

	while(nodePtr!= NULL)
	{
		if(nodePtr->value == memory)
		{
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}
	}

	
}

template<class Object>
Object LinkedList<Object>::getTotal_free_memory()
{
	return total_free_memory;
}

template<class Object>
Object LinkedList<Object>::getTotal_used_memory()
{
	return total_used_memory;
}


template<class Object>
void LinkedList<Object>::appendNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr;

	newNode = new ListNode<Object>(newValue);

	if(head == NULL)
		head = newNode;

	else
	{
		nodePtr = head;

		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::appendNode(Object newValue, int startValue, int endValue)
{
	ListNode<Object> *newNode, *nodePtr;
	
	//Allocate a new node and store the value
	newNode = new ListNode<Object>(newValue, startValue, endValue);

	//If there are no nodes in the list
	//make newNode the first node
	if(head == NULL)
		head = newNode;
	//Insert the node at the end of the list
	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Find the last node in the list
		while(nodePtr->next != NULL)
			nodePtr = nodePtr->next;

		//Insert newNode as the last node
		nodePtr->next = newNode;
	}

}

template<class Object>
void LinkedList<Object>::show_List(string name)
{
	cout<<"*****"<<name<<"*****"<<endl;

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		cout<<nodePtr->value<<": ";

			if(!nodePtr->node_linked_list->empty())
			{	
				for(int i = 0; i != nodePtr->node_linked_list->size(); i++)
				cout<<(*nodePtr->node_linked_list)[i];
			}

		nodePtr = nodePtr->next;
		cout<<endl;
	}

	cout<<""<<endl;
}

template<class Object>
ListNode<Object> LinkedList<Object>::find_value(Object memory)
{

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}	

		nodePtr = nodePtr->next;

	}

	return nodePtr;
}

template<class Object>
void LinkedList<Object>::allocateMemory(ListNode<Object> *node)
{
	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value == node->value)
		{	
			nodePtr->node_linked_list->push_back(node->get_rangeNumber());
			break;
		}	

		nodePtr = nodePtr->next;
	}

}

template<class Object>
void LinkedList<Object>::insertNode(Object newValue)
{
	ListNode<Object> *newNode, *nodePtr, *previousNode = NULL;

	//Allocate a new node and store newValue
	newNode = new ListNode<Object>(newValue);

	//If there are no nodes in the  list
	//make newNode the first node

	if(head == NULL)
	{
		head = newNode;
		newNode->next = NULL;
	}

	else
	{
		//Initialize nodePtr to head of list and previousNode to NULL
		nodePtr = head;
		previousNode = NULL;

		//Skip all nodes whose value member is less than newValue
		while(nodePtr != NULL && nodePtr->value < newValue)
		{
			previousNode = nodePtr;
			nodePtr = nodePtr->next;
		}

		//If the new node is the first in the list
		if(previousNode == NULL)
		{
			head = newNode;
			newNode->next=nodePtr;
		}

		else
		{
			previousNode->next=newNode;
			newNode->next=nodePtr;
		}
	}
}

template<class Object>
void LinkedList<Object>::deleteNode(Object searchValue)
{
	ListNode<Object> *nodePtr, *previousPtr;

	//If the list is empty do nothing
	if(head == NULL)
		return;

	//Determine if the first node is the value that we are looking for
	if(head->value == searchValue)
	{
		nodePtr = head->next;
		delete head;
		head = nodePtr;
	}

	else
	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//Skip all nodes whose value member is not equal to search value
		while(nodePtr != NULL && nodePtr->value != searchValue)
		{
			previousNode=nodePtr;
			nodePtr = nodePtr->next;
		}

		//If nodePtr is @ the end of the list
		//than link the previous node to the node after nodePtr
		if(nodePtr)
		{
			previousNode->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

template<class Object>
LinkedList<Object>::~LinkedList()
{
	ListNode<Object> *nodePtr, *nextNode;

	nodePtr = head;
	while(nodePtr != NULL)
	{
		nextNode = nodePtr->next;
		delete nodePtr;
		nodePtr = nextNode;

	}
}

#endif

Open in new window


BuddyMemory.h
 
#ifndef BUDDYMEMORY_H
#define BUDDYMEMORY_H

#include<iostream>

using namespace std;
#include"LinkedList.h"

class BuddyMemory
{
public:
	BuddyMemory();
	~BuddyMemory();
	
	void check_free_memory(int);

	void addTo_allocated_list();

	void addTo_free_list();

private:
	LinkedList<int> *allocated_list;
	LinkedList<int> *free_list;

};
#endif

Open in new window


BuddyMemory.cpp
 
#include<iostream>

using namespace std;

#include"BuddyMemory.h"
#include"LinkedList.h"

BuddyMemory::BuddyMemory()
{
	//Create allocated_list
	allocated_list->appendNode(256);
	allocated_list->appendNode(128);
	allocated_list->appendNode(64);
	allocated_list->appendNode(32);
	allocated_list->appendNode(16);
	allocated_list->appendNode(8);
	allocated_list->appendNode(4);
	allocated_list->appendNode(2);

	//Create free_list
	free_list->appendNode(256, 0,32);
	free_list->appendNode(128, 33, 97);
	free_list->appendNode(64, 98, 226);
	free_list->appendNode(32, 227, 483);
	free_list->appendNode(16, 484, 996);
	free_list->appendNode(8, 997, 2021);
	free_list->appendNode(4, 2022, 4070);
	free_list->appendNode(2, 4076, 80167);
	
	free_list->set_memory(256);

	free_list->show_List("Free List");
	allocated_list->show_List("Allocated List");

}

BuddyMemory::~BuddyMemory()
{
	 allocated_list->~LinkedList();
	 free_list->~LinkedList();
}

void BuddyMemory::check_free_memory(int value)
{
	if(free_list->getTotal_used_memory()+ value <= 256)
	{
			
			allocated_list->allocateMemory(&free_list->find_value(value));
			
			free_list->show_List("Free List");
	}
	else
		cout<<"You Must Free Up Space"<<endl;	
}

Open in new window


Main.cpp
 
#include<iostream>
#include<vector>
#include<string>

using namespace std;

#include"BuddyMemory.h"

int main()
{
	cout<<"***********BUDDY MEMORY PROGRAM**********\n"<<endl;

	BuddyMemory start_program;

	int memory;
	int choice=1;


	do
	{
			
		cout<<"Enter in corresponding number"<<endl;
		cout<<"1. Allocate Memory"<<endl;
		cout<<"2. Delete Memory"<<endl;
		cout<<"3. Exit Program"<<endl;
	    
		cin>>choice;
		


	if(choice == 1)
	{
			cout<<"Enter In The Amount Of Memory You Would Like To Allocate\n"<<endl;

			cin>>memory;

			while((memory <= 0) || (memory > 256))
					{
							cout<<"Oops, Please Enter In An Amount Between '2 - 256' \n"<<endl;

							cin>>memory;

					}//End Of While
		
			
			start_program.check_free_memory(memory);
			
				
	}
	
	
		/*else if(choice==2)
		{
			cout<<"How Much Memory Would You Like To Delete"<<endl;
			cin>>memory;

				while((memory <= 0) || (memory > 256))
				{
					cout<<"Oops, Please Enter In An Amount Between '2 - 256' \n"<<endl;
					cin>>memory;
				}

				allocatedList.deleteMemory(memory);


		}*/
	
	}while(choice != 3);


	return 0;
}//End Of Main

Open in new window

0
Comment
Question by:brich744
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35175838
You're trying to return a pointer from a function that expects to return an object. Pointers are treated as integers, so that's why the error message seems a bit strange.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35175925
Shpuldn't you return the 'value' member of the 'nodePtr' that you've found? I.e.
template<class Object>
ListNode<Object> LinkedList<Object>::find_value(Object memory)
{

	ListNode<Object> *nodePtr;
	nodePtr = head;
	while(nodePtr != NULL)
	{
		if(nodePtr->value>= memory && (nodePtr->next->value<memory || nodePtr->next->value == NULL ))
		{	
			nodePtr->set_rangeNumber();
			nodePtr->node_linked_list->push_back(nodePtr->get_rangeNumber());
			break;
		}	

		nodePtr = nodePtr->next;

	}

	return nodePtr->value; // <---- here
}

Open in new window



At least the return type of 'ListNode<Object>' suggests that - and it compiles that way.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35175938
>> You're trying to return a pointer from a function that expects to return an object.
So, what to do?
ListNode<Object> LinkedList<Object>::find_value(Object memory)
   ...
   return nodePtr;

// USAGE:
     allocated_list->allocateMemory(&free_list->find_value(value));
// WHERE
void LinkedList<Object>::allocateMemory(ListNode<Object> *node)

Open in new window

If you fix the return statement so as to return an object, then you are copying that object to the caller. Not a good idea if that object is large.

Passing pointers are faster to pass (references too). But if you do that, then you will have to change the USAGE that I noted above.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:brich744
ID: 35176123
That is what I am having an issue with I have changed the

ListNode<Object> LinkedList<Object>::find_value(Object memory)
   ...
   return *nodePtr;

and also I have tried to change the USAGE to

 allocated_list->allocateMemory(free_list->find_value(value));

I guess I am having an issue with the proper syntax.

0
 
LVL 86

Expert Comment

by:jkr
ID: 35176145
Have you tried changing that to

return nodePtr->value;

as I suggested above?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 35176182
In OP, as kaufmed explained, in find_value, you "return a pointer from a function that expects to return an object". So, you fixed this problem with:
     return *nodePtr; // now you are returning an object (good, but potentially inefficient for large objects)

allocateMemory expects a pointer to a ListNode, and you originally had
    allocated_list->allocateMemory( &   free_list->find_value(value));
So, all is good. It compiles with just the one change to the return statement.

If you want to return a pointer instead, then your new statement (without the &) would be good:
     allocated_list->allocateMemory(free_list->find_value(value));
That is what I meant by changing the USAGE - i.e., remove the & if returning a pointer.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 35179449
you can't return

  nodePtr->value;


from find_node function cause the nodePtr can be NULL.

generally a find function must have the option to return 'not found' what in the above case is either it returns an 'empty' ListNode<Object> or the return type would be changed to a pointer to ListNode<Object> which then could be NULL if value was not found and the found pointer else.

Sara
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now