Thread: Lost ID number

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Lost ID number

    I´m currently working on a phonebook and I´m trying to create a unique ID number for each entry. It looks something like this...

    Code:
    class Person
    {
    public:
    	Person(){itsId = id++;}
    	~Person();
           ............
           .............
    	void SetNext(Person *setlink) {next = setlink;}
    	void SetPrevious(Person *setlink) {previous = setlink;}
    	
    	Person* GetNext() const {return next;}
    	Person* GetPrevious() const {return previous;}
    
    	int getId() {return itsId;}
    private:
    	static int id;
    	int itsId;
    	//For double-linked list
    	Person *next;
    	Person *previous;
    };
    
    int Person::id = 0;
    I have stored all entries in a double linked list because I don't know how many entries the user is gonna input. The problem is is when an entry gets deleted that corresponing ID number is lost.
    I´m trying to find a solution that I can reuse this "lost ID number", but so far haven´t been sucessful.

    Any suggestion????

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I would make an id simply by taking an int say 1000 and constantly adding 1 to it for new ids.
    My allocID() routine would check a queue for an id and if queue is empty then alloc id as above.
    My deallocID() would simply push the dealloced id onto the queue.

    It can be that simple.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    It can be that simple.
    I wish that , but of course when I try to implement it it doesn´t work!!!

    When I declare a queue in main like this, it works!
    Code:
    #include <iostream>
    #include <queue>
    
    using namespace std;
    
    int main()
    {
    	queue<int> aqueue;
    
    	aqueue.push(12);
    	aqueue.push(123);
    	aqueue.push(1234);
    	aqueue.push(12345);
    	
    	int aqueuesize = aqueue.size();
    
    	for (int i = 0; i < aqueuesize; i++)
    	{
    		cout << "Value nr " << i+1 << " " << aqueue.front() << endl;
    		aqueue.pop();
    	}
    return 0;
    }
    but in mine Person-class it doesn´t

    Code:
    Person::Person()
    {
                    //If queue is empty then do as usual
    	if (idqueue.empty())
    		itsId = id++;
    	//Or if it isn´t
                    else
    	{
                                    //retrieve 'lost id number' (front item)
    		itsId = idqueue.front();
                                    //remove the front item
    		idqueue.pop();
    	}
    }
    
    Person::~Person()
    {
                    //When deleting a person push that id on a queue
    	idqueue.push(itsId);
    }
    When I print out some debug I´ve noticed some strange things, in the deconstructor of Person it pushes the right value every time but the size of the queue reamains at 1 all the time. I´ve deleted
    like 10 Persons one after one but still it insist that only 1 element is in the queue.
    And every time the constructor is invoked the queue seems to be empty.

    What am I doing wrong???

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The queue should be a static member.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Why does idqueue have to be static? I think declaring it global or passing it to the constructor as an argument would also work.

    Also where do you get id from? Since it's not passed in it must be either a global variable or a static variable, correct?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    making the queue static to person would help with the idea of encapsulation. Why make something global if it doesnt need to be. The other point of it being static is you need only 1 queue for the class and not one per object. Its just the tidy way of doing things.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    What are you using the ID for, as an index value??

    Otherwise, if your not using it as an index value why do you need to reuse the number. Make the ID value a product of the DATE, use the date to generate a unique ID.

    If you're using the ID as an index value then just record the ID num of the record your deleting and store it in an array or file and then you can just reuse those recorded deleted values as ID's for the newer records.

    You'll also need a re-indexer function to repair the index values incase of error and the values are out of sync.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  8. #8
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    What are you using the ID for, as an index value??
    It makes it easier for the user to delete a Person.

    The queue should be a static member.
    Of course it should be, where is my head???

    I have made the queue static now but now I get a link-error. "unresolved external symbol "public: static class std::queue<int,class std::deque<int,class std::allocator<int> > > Person::idqueue" (?idqueue@Person@@2V?$queue@HV?$deque@HV?$allocato r@H@std@@@std@@@std@@A)
    Debug/Phonebook with a linked list.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe."

    Code:
    class Person
    {
    public:
    	Person();
    	~Person();
           ............
           .............
    	void SetNext(Person *setlink) {next = setlink;}
    	void SetPrevious(Person *setlink) {previous = setlink;}
    	
    	Person* GetNext() const {return next;}
    	Person* GetPrevious() const {return previous;}
    
    	int getId() {return itsId;}
    private:
    static queue <int> idqueue;//This static causes a linker error
    	static int id;
    	int itsId;
    	//For double-linked list
    	Person *next;
    	Person *previous;
    };
    
    int Person::id = 0;
    Last edited by ripper079; 10-03-2002 at 11:22 AM.

  9. #9
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Anyone???

  10. #10
    Registered User
    Join Date
    Mar 2002
    Location
    South Africa
    Posts
    35
    How about this: When you delete a person (this should be done from within the class!), remove its idnumber from the queue (or use a _sorted_ list, it removes more easily). When you add a new person, run through the queue/list, checking where the first gap is. Then use the number that used to fill the gap.
    I code.
    I think.
    Ergo, I think in code.

  11. #11
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    But that is what my code does. I need one queue for all objects, but when I try to declare my idqueue to be static (se code)I get a linker error. I can compile my source code but not link it.

  12. #12
    Why can't you do what I suggested and SAVE the ID nums that are deleted in a file, array, or Variable. That way you know what was deleted and can use the values in the file to re-issue those deleted ID's to new Objs. created.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  13. #13
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Originally posted by OneStiffRod
    Why can't you do what I suggested and SAVE the ID nums that are deleted in a file, array, or Variable. That way you know what was deleted and can use the values in the file to re-issue those deleted ID's to new Objs. created.


    ???

    But that is exacly what I'm doing (I´m pushing all deleted id-numbers to a queue). Here is code related to idnumber.

    Code:
    class Person
    {
    public:
    	Person();
    	~Person();
           ............
           .............
    	void SetNext(Person *setlink) {next = setlink;}
    	void SetPrevious(Person *setlink) {previous = setlink;}
    	
    	Person* GetNext() const {return next;}
    	Person* GetPrevious() const {return previous;}
    
    	int getId() {return itsId;}
    private:
    static queue <int> idqueue;//This static causes a linker error
    	static int id;
    	int itsId;
    	//For double-linked list
    	Person *next;
    	Person *previous;
    };
    
    int Person::id = 0;
    
    Person::Person()
    {
                    //If queue is empty then do as usual
    	if (idqueue.empty())
    		itsId = id++;
    	//Or if the queue contains something
    	else
                    {
                                    //retrieve 'lost id number' (front item)
    		itsId = idqueue.front();
                                    //remove the front item
    		idqueue.pop();
    	}
    }
    
    Person::~Person()
    {
                    //When deleting a person push that id on a queue
    	idqueue.push(itsId);
    }
    The code compiles with no problem but insteed a get a linker error (read my last reply). I know what is causing it (static declaration of the queue) but I don´t know how to solve it.

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You need to define the static member:

    Code:
    class Person
    {
    public:
        /*......*/
    private:
        static queue <int> idqueue;
        static int id;
        /*.......*/
    };
    
    //You did this with the int
    int Person::id = 0;
    //It is also needed with the queue
    queue<int> Person::idqueue;
    This should solve your problems
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM