Advertisement

Can anyone help with stack?!

Started by September 24, 2000 01:12 PM
6 comments, last by Webxcraz 24 years, 1 month ago
I can''t seem to convert this function from c to c++. int push(int which, int size) { stack_node *node = malloc(sizeof(stack_node)); node->size = size; node->next = poles[which].head; poles[which].head = node; poles[which].depth++; return 0; } here is the structure: typedef struct stack_node { int size; struct stack_node *next; } stack_node; typedef struct stack { struct stack_node *head; int depth; } stack; stack poles[4]; any help would be greatly appretiated
Argue with an idiot, he''ll drag you down to his level, then beat you with experience
When declaring a variable whose type is a struct you don''t have to write struct before it... Dunno if that makes any difference... Maybe a couple of other things... see if this helps...

ie, from your example...

    typedef struct _stack_node{  int size;  inline stack_node *next;} stack_node;typedef struct _stack{  stack_node *head;  int depth;} stack;    


Dunno if that works at all, but you can try it. You function looks fine, I suppose... Unless it''s giving you error messages... If it is then you should probably post them.

S.
Advertisement
Thanks but the sturture works fine, the function is what gives me problems. I get this error message:
: error C2440: ''initializing'' : cannot convert from ''void *'' to ''struct stack_node *''
Conversion from ''void*'' to pointer to non-''void'' requires an explicit cast
Error executing cl.exe.
Argue with an idiot, he''ll drag you down to his level, then beat you with experience
If I use inline in place of struct I get error:

: error C2433: 'next' : 'inline' not permitted on data declarations

I'm using MSVC++ 6.0

Edited by - webxcraz on September 24, 2000 2:55:58 PM
Argue with an idiot, he''ll drag you down to his level, then beat you with experience
stack_node *node = malloc(sizeof(stack_node));

As the error says, it requires an explicit cast...

stack_node *node = (stack_node*)malloc(sizeof(stack_node));

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
quote: Original post by Webxcraz

If I use inline in place of struct I get error:

: error C2433: ''next'' : ''inline'' not permitted on data declarations

I''m using MSVC++ 6.0

Edited by - webxcraz on September 24, 2000 2:55:58 PM


You can only inline functions. Not data types...that just doesn''t make sense

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Advertisement
Ya, I thought that inline thing was only for functions, I just suggested it because it might have worked for some odd reason.

S.
thanks guys for your help...function is working!
Argue with an idiot, he''ll drag you down to his level, then beat you with experience

This topic is closed to new replies.

Advertisement