Sunday, 15 July 2012

Overloading `--` operator is not working in C++ on Mac -



Overloading `--` operator is not working in C++ on Mac -

i trying overload operators in c++. creating custom stack info structure.

this stack.h in headers directory:

/******************************************************************************* * stack -- * * class interface stack of integers. * * stack linear info structure. elements pushed stack * * bottom , popped top. * * * * author -- aditya r.singh * * version -- 1.0 * * since -- 2014-06-24 * *******************************************************************************/ #ifndef stack_h #define stack_h class stack { public: stack(); // initiallize stuff. void operator>>(int); // force elements in stack. int operator--(); // pop element stack. void display_stack(); // display whole stack. private: typedef struct stack { int data; struct stack *top, *previous; } node; // construction of whole stack. node *ptr; // point top of stack. node *buffer; // temporarily store node freed. int data; // info popped stack. int choice; // selection of user. }; #endif

my stack.cpp file, in sources directory is:

/******************************************************************************* * stack -- * * file contains implementation of functions class stack.* * * * author -- aditya r.singh * * version -- 1.0 * * since -- 2014-06-24 * *******************************************************************************/ #include <iostream> #include "../headers/stack.h" using namespace std; stack::stack() { ptr->top = null; ptr->previous = null; } void stack::operator>>(int data) { ptr = (node*)malloc(sizeof(node)); ptr->previous = ptr->top; ptr->top = ptr; ptr->data = data; } int stack::operator--() { buffer = ptr->top; info = buffer->data; ptr->top = ptr->previous; free(buffer); homecoming data; } void stack::display_stack() { buffer = ptr; while(buffer != null) { cout << buffer->data << endl; buffer = buffer->previous; } }

my main.cpp in sources directory:

/******************************************************************************* * main -- * * programme demonstrate utilize of stack. * * * * author -- aditya r.singh * * version -- 1.0 * * since -- 2014-06-24 * *******************************************************************************/ #include <iostream> #include "../headers/stack.h" using namespace std; int get_choice() { int selection = 0; cout << "*************************************" << endl; cout << "************ stack ***********" << endl; cout << "*************************************" << endl; cout << "********** 0> quit app *********" << endl; cout << "********** 1> force *********" << endl; cout << "********** 2> pop *********" << endl; cout << "********** 3> display stack *********" << endl; cout << "********** 4> clear console *********" << endl; cout << "*************************************" << endl; cout << endl << "enter selection = "; cin >> choice; homecoming choice; } int main() { stack stack; int selection = -1; int data; while(choice) { selection = get_choice(); switch(choice) { case 1: cout << "enter number pushed = "; cin >> data; stack >> data; break; case 2: cout << "just pushed " << stack-- << endl; break; case 3: stack.display_stack(); break; case 4: system("clear"); break; default: cout << "invalid choice!" << endl; break; } } homecoming 0; }

my headers, sources , objects directories in stack directory. terminal pointing in stack directory. compiled stack.cpp file this:

gcc -c sources/stack.cpp -o objects/stack.o

it compiled fine , produced stack.o file in objects directory.

but when compiled main.cpp file this:

gcc sources/main -o stack objects/stack.o

it's giving me next error:

main.cpp:66:43: error: cannot decrement value of type 'stack' case 2: cout << "just pushed " << stack-- << endl;

it's finding problem when trying --(decrement) value of stack. overloaded -- operator in class stack. possible problem?

please help.

you have overloaded pre-decrement operator int operator--(); using post-decrement operator, has overload signature int operator--(int); int parameter token distinguish between prefix , suffix version.

c++ operator-overloading

No comments:

Post a Comment