-
Errors please help
I'm trying to finish up some homework, and keep getting the "invalid conversion from `char' to `const char*' " error. I'm trying to insert into a linear linked list, any help would be appericated. Heres the code, I hope its enough of the puzzle.
Code:
food_TYPE *data;
node *type;
head = new node;
current = new node;
current = head;
head->next = NULL;
node *temp;
temp = new node;
int len;
if(temp)
{
//copy into node
strcpy (current->data->food, foodm);
strcpy(current->food_TYPE->location, location);
current->data.rating = rating;
}
-
Code:
head = new node;
current = new node;
current = head;
This is a memory leak. You dynamically allocate some new memory to current, then you throw it away when you point current to head. Other than that, it sounds like you problem may be with foodm or location. Show how they are declared along with the declarations for your food_TYPE and node objects.
-
foodm and location are both in the main, we're implementing ADT's, and cant cin or cout from a class, so I'm trying to pass them from the main to the class.
The struct food_TYPE is declared in struct.h and struct node is declared in class.h
Code:
struct food_TYPE
{
char *food; //what type of food is sold
char *location; // where food is served
int rating; // rating of how good food is
// node *next;
};
struct node
{
struct food_TYPE *data;
node *next;
};
-
I figured I would post all the code I have for the insert, so you can hopefully figure out where I was headed with this.
Code:
//******************************************************************************
////////////////////////////////////////////////////////////////////////////////
//class code to insert in sorted order
int resturant::add_Resturant(char &foodm, char &location, int rating)//, food_TYPE &head, food_TYPE &next)
{
resturant Resturant;
food_TYPE *data;
node *type;
head = new node;
current = head;
head->next = NULL;
node *temp;
temp = new node;
int len;
if(temp)
{
//copy into node
strcpy (current->data->food, foodm);
strcpy(current->data->location, location);
current->data->rating = rating;
}
else return 0;
if(!head)
{
head = temp;
head -> next = NULL;
}
else if (strcmp(head->data, temp->data) >0)
{
temp->next = head;
head = temp;
}
else
{
node *current = head ->next;
node *previous = head;
}
while(strcmp(current->data, temp->data)<0)
{
previous = current;
current = current ->next;
}temp->next = current;
//delete dynamic memory
delete data->food;
delete data->location;
}
-
in your function parameters change 'char &foodm' to 'char* foodm'
-
I just wanted to say thanks to all those who responded, I was able to get the program running and turned in, thanks.