Link to home
Start Free TrialLog in
Avatar of brich744
brich744

asked on

Error: C2664

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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.
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.
Avatar of phoffric
phoffric

>> 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.
Avatar of brich744

ASKER

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.

Have you tried changing that to

return nodePtr->value;

as I suggested above?
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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