Thread: Help with Class and Pointer Syntax

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    21

    Help with Class and Pointer Syntax

    Hi,

    I have a class with a constructor
    Code:
    HTblock(int width, COLORREF color);
    I also have a stack of pointers of type HTblock called tower. I need to Apply Draw() function for each HTblock in the "tower" stack as in
    Code:
    block_pointer->Draw(m_window,x,y);
    im having trouble getting this to work as i've tried different methods to do this including assigning a temporary variable to get from the tower.push() call.
    however i cant seem to get a syntax that works. I can clarify if i am not clear in what im trying to do.

    Help would be much appreciated,
    Thanks,
    Cram
    Last edited by cram; 11-16-2004 at 09:22 PM.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    It'll be hard to diagnose without some more code. Minimally,
    a) What stack are you using? Are you using an adapted standard list, or your own? If its the latter, can you post the interface.
    b) The code where you try to call the function.
    c) The code that initializes your stack (esp. if it is templated).

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    So are you saying you need to temporarily pop() each HTblock off tower, do a Draw, then push back onto the stack?

    If yes, then maybe you need a second stack to store each HTblock as you pop it. Something like:
    Code:
    stack<HTblock> Stack;
    while (!tower.empty())
    {
       HTblock item = tower.pop();
       item.Draw();
       Stack.push(item);
    }

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    push() usually adds an element to a stack, whereas pop() usually removes the last element added to the stack. It depends on how the stack is implemented what the return value of pop() is. In the STL stack class pop() has return type void, meaning you must call top() to get the value of the top element in the stack, then call pop() if you want to remove it.

    tower.top()->Draw(m_window, x, y);
    tower.pop();

    might work if you are using the STL stack class. Like Zach said, if you have created your own stack class, then we will need to see how you have implemented the pop() function.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    Quote Originally Posted by swoopy
    So are you saying you need to temporarily pop() each HTblock off tower, do a Draw, then push back onto the stack?

    If yes, then maybe you need a second stack to store each HTblock as you pop it. Something like:
    Code:
    stack<HTblock> Stack;
    while (!tower.empty())
    {
       HTblock item = tower.pop();
       item.Draw();
       Stack.push(item);
    }
    This is essentailly what i have to do (sorry I wasnt clearer) however my code isnt working. This is my code:
    Code:
    Stack *temptower = (Stack *) new Stack;
    	for (int j = 0; j < (tower->GetLength()); j++){
    	temptower->Push(tower->Pop());
    	}	
    	HTblock *temp_block;
    	for (int k = 0; k < (temptower->GetLength()); k++){
    	temp_block = temptower->Pop();
    	temp_block->Draw(m_window, x_base, (y_base - (k * 20)));
    	tower->Push(temp_block);
    	}
    where tower is the stack originally with the HTblock pointers. However when i go to compile this the third last line gives me an error saying "=, cannot convert from StackItemType to HTblock", if need be i can post the stack code, thanks for the help,
    Cram

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    As a side note, the first line need only be 'Stack* temptower = new Stack;'.
    What is the StackItemType (is it a typedef in the Stack class?). It would be helpful to see your stack code.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    okay here is my stack code, unfortunetly it uses a list class but hopefully you should be able to figure it out without seeing the list code:
    Stack header:
    Code:
    #pragma once
    #include "list.h"
    
    typedef ListItemType StackItemType;
    
    class Stack
    {
    public:
    	Stack(void);
    	~Stack(void);
    	void Push(StackItemType item);
    	StackItemType Pop(void);
    	StackItemType Peek(void);
    	inline bool IsEmpty() const { return m_container.IsEmpty();}
    	inline bool IsFull()  const { return m_container.IsFull();}
    	inline int GetLength() const { return m_container.GetLength();}
    private:
    	int m_top;
    	List m_container;
    };
    Stack Source Code:
    Code:
    #include "StdAfx.h"
    #include ".\stack.h"
    
    Stack::Stack(void)
    {
    	m_top = 0;
    }
    
    Stack::~Stack(void)
    {
    }
    
    void Stack::Push(StackItemType item)
    {
    	m_top++;
    	m_container.Insert(m_top, (ListItemType) item );
    }
    
    StackItemType Stack::Pop(void)
    {
    	StackItemType s;
    	if (IsEmpty()) return NULL;
    	m_container.Retrieve(m_top, (ListItemType *) &s );
    	m_container.Remove(m_top);
    	m_top--;
    	return s;
    }
    
    StackItemType Stack::Peek(void)
    {
    	StackItemType s;
    	if (IsEmpty()) return NULL;
    	m_container.Retrieve( m_top, (ListItemType *) &s );
    	return s;
    }
    If you could solve my question thatd really help me out thanks

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If I understand correctly, you have a stack, and you want to draw the whole thing, and then have the stack the way it was before you drew it. If so, do as swoopy suggested:
    Code:
    Stack tempstack;
    StackItem item;
    while( !origionalstack.isEmpty() )
    {
        item = origionalstack.pop();
        draw( item );
        tempstack.push( item );
    }
    
    while( !tempstack.isEmpty() )
    {
        origionalstack.push( tempstack.pop() );
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    Quote Originally Posted by quzah
    If I understand correctly, you have a stack, and you want to draw the whole thing, and then have the stack the way it was before you drew it. If so, do as swoopy suggested:
    Code:
    Stack tempstack;
    StackItem item;
    while( !origionalstack.isEmpty() )
    {
        item = origionalstack.pop();
        draw( item );
        tempstack.push( item );
    }
    
    while( !tempstack.isEmpty() )
    {
        origionalstack.push( tempstack.pop() );
    }
    Quzah.
    essentaially thats what ive done except with the pointer stuff its not working. This line
    Code:
    item = origionalstack.pop();
    is not functioning. Thanks

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If this situation is not something unusual in your app (e.g. some quick functionality testing) than obviously your data structure is the wrong choice. What you need here is a stack that still allows you access to each stack element. That would solve the problem in a good way. Hacking around a deficiency in your container choice is not a good solution.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    where tower is the stack originally with the HTblock pointers. However when i go to compile this the third last line gives me an error saying "=, cannot convert from StackItemType to HTblock", if need be i can post the stack code, thanks for the help,
    Here's a puzzling question. How did you create tower with HTblock pointers? If tower is a stack, it would consist of StackItemType's or ListItemType's. It appears from this error:
    "=, cannot convert from StackItemType to HTblock"
    that HTblock is not the same as a StackItemType.

    Now I suppose if they are the same size you could do a cast:
    temp_block = (HTblock) temptower->Pop();
    or if it's a pointer:
    temp_block = (HTblock *) temptower->Pop();

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not so sure about that "if they're the same size" thing:
    Code:
    struct foo
    {
        unsigned long a;
        double b;
    };
    
    struct bar {
        double b;
        unsigned long a;
    };
    
    bar b;
    foo f;
    
    f = (struct foo)b;
    Probably wouldn't give you what you wanted.

    [edit]But yeah, you're probably dead on aside from the "size" portion.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    21
    Quote Originally Posted by swoopy
    Here's a puzzling question. How did you create tower with HTblock pointers? If tower is a stack, it would consist of StackItemType's or ListItemType's. It appears from this error:
    "=, cannot convert from StackItemType to HTblock"
    that HTblock is not the same as a StackItemType.

    Now I suppose if they are the same size you could do a cast:
    temp_block = (HTblock) temptower->Pop();
    or if it's a pointer:
    temp_block = (HTblock *) temptower->Pop();
    yah that last line seems to work thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM