linked lists, strcmp(), char array, & a structure

This is a discussion on linked lists, strcmp(), char array, & a structure within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use information that is stored in a linked list node to compare it to a key. The ...

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question linked lists, strcmp(), char array, & a structure

    I'm trying to use information that is stored in a linked list node to compare it to a key. The data in the node is a char array in a structure. The key is a user inputed char array. I want to compare the two for a sort of the linked list. Last name to Last name. It doesn't work. I'm unable to see why it doesn't work.

    Below is the code I'm using.
    Code:
    //prototype 
    bool compare(const char*,const char*);
    
    //key declaration
    char  Key[SIZE];
    
    //structure definition
    struct link                          
    {
    short age;
    short weight;
    char fname[SIZE];
    char lname[SIZE];
    char ssnumber[SIZE];
    link *next;                       
    };
    
    //my function call
    compare(current->lname,Key)
    
    //definition function
    bool compare(const char* node,const char* key)
    {
    	if(strcmp(node,key))
    		return true;
    	else
    		return false;
    }
    I get the following errors
    (1)linked lists error LNK2019: unresolved external symbol "private: bool __thiscall linklist::compare(char const *,char const *)" (?compare@linklist@@AAE_NPBD0@Z) referenced in function "public: void __thiscall linklist::delete_key(void)" (?delete_key@linklist@@QAEXXZ)
    (2)linked lists fatal error LNK1120: 1 unresolved externals
    Last edited by xviddivxoggmp3; 10-04-2003 at 03:43 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    strcmp() returns 0 when the strings are equal

    So perhaps
    Code:
    bool compare(const char* node,const char* key)
    {
    	if(strcmp(node,key) == 0)
    		return true;
    	else
    		return false;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    no work.

    No that didn't work.
    It says I have unresolved external error.
    linking problem. please see the bottom of the prior post for the exact errors.

    [Edit]
    It is acting as if it will not recognize a bool function call within a void function. Does this even matter?
    [\Edit]
    Last edited by xviddivxoggmp3; 10-04-2003 at 03:51 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I can't see anything wrong with the code you posted there...must be a problem somewhere else ...

    It is acting as if it will not recognize a bool function call within a void function. Does this even matter?
    I don't see why it would matter...

    [edit]
    compare(current->lname,Key);
    ...dont know if that fixes it though
    [/edit]
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Man, you gotta start being specific about what the actual problem is. You post some code with an obvious flaw, then totally throw a different problem at us.

    Try
    bool linklist::compare(const char* node,const char* key)

    Make it a member function of your class, and not a global function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    sorry about that.

    I thought I was being specific. I figured it out though.
    The issue was created by me thinking I had to create a function that is passed two constant pointers for strcmp() to work.
    I recognized that that was not needed. The issues arose from calling the function with passing the linked data and a char key.
    I have since then tried to solve the problem without passing to another function and it works perfectly. see the below code. I have a book that states that all string functions like strcmp() needs to have 2 const pointers passed for it to work, but it works with plain pointers with out making them constant. So the book is not correct.
    Code:
     
    void linklist::delete_key()
    {
    					
    	cout << "Please enter the last name of member you wish to delete: "<<endl;
    	cin >> Key;
    
    	int counter=1,keydelete=false;
    	char test[SIZE];
    
    	
    	if (isempty())
    	{
    		cout << "Sorry, the list is empty. Nothing to delete." << endl;
    		keydelete=false;
    	}
    	link * current , * previous;
    	current = previous = head;
    	while (current != NULL)
    	{//below here is where I had the function call that was not working. 
    		if ( strcmp(current->lname,Key)==0  )
    		{
    			previous->next = current->next;
    			delete current;
    			break;
    		}
    		counter++;
    		previous = current;
    		current = current->next;
    	}
    
    	if (current == NULL)
    	{
    		cout<<"Key is not in list";
    		keydelete=false;
    	}
    	else
    		keydelete=true;			   
    	
    	if(keydelete)
    		cout << "Node key " << Key << " has been deleted.";
    }
    [edit]
    compare(current->lname,Key);
    ...dont know if that fixes it though
    [/edit]
    sorry that was just a typo.
    Last edited by xviddivxoggmp3; 10-04-2003 at 07:29 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fill a linked list char by char
    By w2look in forum C Programming
    Replies: 6
    Last Post: 03-26-2009, 04:07 PM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 05:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 09:57 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21