Thread: Problem with "this" pointer

  1. #1
    Unregistered
    Guest

    Problem with "this" pointer

    hi I am trying to create a linked list class. My problem is the "this" pointer. See, because the member functions cannot refer to the immediate variable instance by name, I must use the "this" pointer. Look at it so far, with only one member function complete:


    Code:
    
    class LinkedList
    {
    public:
    
    int AddTo ( LinkedList *head ) {
    
    this = new LinkedList;  //<-- ERROR: NON-LVALUE IN ASSIGNMENT
    
    if( !this ) return 0;
    
    if( !head ) {
    head = this;
    head->prev = 0;
    head->next = 0;
    return 1; }
    
    LinkedList *b = head;
    
    int nodeCount = 1;
    
    while( b->next ) {
    b = b->next;
    nodeCount++; }
    
    b->next = this;
    this->prev = b;
    
    return nodeCount; }
    
    
    LinkedList *prev;    LinkedList *next;
    };

    I have tried everything, but it just will not compile.

    Can anyone give me some direction?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    That code needs a full reworking. You can refer to members of an object using the this pointer. what you are looking for is something like....

    this->next=new LinkedList;

    etc.
    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
    Unregistered
    Guest
    You are right, but I need to allocate for "this" instance, since all objects will be declared as pointers to deallocate. Wait a minute. If C++ class instances get ~destructors, then C++ objects can be allocated/deallocated like a pointer while declaring concrete intances--right? So in essence, don't use pointer intances, just call the ~destructor on a real object. Is that correct?

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    this is read only. You have to find another solution. I made a linked list class that contained a pointer to a listItem structure, the definition as such:

    struct listItem
    {
    int value;
    listItem *next;
    }

    That paradigm will work.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    linked list

    I see your problem.. one of them anyway.
    You should separate your code from your declaration file....


    you are declaring
    while and if functions as members of your class....
    you should name them something...

    also you should avoid manipulating the 'this' pointer always no matter how tempting...

    never do this

    delete this;
    this = .......
    you do not own 'this'....
    this is ok
    this->MemberFunction();
    return *this;
    return this->memberData;
    data = this->memberData;

    ......................

    this constructs itselt upon declaration .....
    and as the previous message... use a separate class or struct for the listNodeType....
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM