DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Stack with Dynamic Array (http://www.daniweb.com/forums/thread159678.html)

Lotus_2011 Nov 27th, 2008 3:16 am
Stack with Dynamic Array
 
Hi all...
I tried to maked Stack with Dynamic Array but ........

class Stack
{
private:
        int *SS;
        int size;
        int t;
        void Expand();
public:
        //constractors
        Stack();
        ~Stack();
        Stack(const Stack & Original);
        //operations
        Stack &operator= (const Stack& RHS);
        bool Empty();
        void Display();
        int Pop();
        void Push(int Value);
        int Top ();
};
using namespace std;
#include "stdafx.h"
#include"stack.h"
#include<iostream>

Stack ::Stack ()
{
        size=20;
        SS=new int [size];
        t=-1;
}
Stack ::Stack(const Stack &Original)
{
        size=Original.size;
        t=Original.t;
        SS=new int [Size];
        if(SS!=NULL)
        {
                for(int i=0 ; i<t; i++)
                {
                        SS[i] =Original.SS[i];
                }
        }
}     

bool Stack ::Empty()
{
        return (t==-1);
}
void Stack ::Expand()
{
        size=size*2;
        int * SS_1=new int [size];
        for(int i=0; i<t ; i++)
        {
                SS_2[i]=SS[i];
        }
        delete[]SS;
        SS_2=SS;
}
Stack& Stack ::operator=(const Stack & RHS)
{
        if(this != RHS)
        {
                if(size !=RHS.size)
                {
                        delete[]SS;
                        size=RHS.size;
                        int * SS_2 = new int [size];
                }
                t=RHS.t;
                for(int i=0 ; i<t; i++)
                {
                        SS_2[i] = RHS.SS[i];
                }
        }
        return *this;
}
void Stack :: Push(int Value)
{
        if(t==capacity-1)
                Expand();
        t++;
        SS[t]= Value;
}
int Stack :: Pop()
{
        int V ;
        if(!Empty())
        {
                V=SS[t] ;
                t--;
                return V ;
        }
       
}
int Stack :: Top ()
{
        if(!Empty())
                return (SS[t]);
}

void Stack ::Display()
{
        for(int i=0; i<t; i++)
                cout<<SS[i]<<" ";
}


Stack ::~Stack()
{
        delete[]SS;
}
succed when main is empty
#include "stdafx.h"
#include"stack.h"

int _tmain(int argc, _TCHAR* argv[])
{
        return 0;
}

when i tried to write in main .......
#include "stdafx.h"
#include"stack.h"
int _tmain(int argc, _TCHAR* argv[])
{
        Stack S;
        return 0;
}
3_synatx error apper to me

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Stack::~Stack(void)" (??1Stack@@QAE@XZ) referenced in function _wmain Stack.obj

Error 2 error LNK2019: unresolved external symbol "public: __thiscall Stack::Stack(void)" (??0Stack@@QAE@XZ) referenced in function _wmain Stack.obj

Error 3 fatal error LNK1120: 2 unresolved externals E:\YOZOO\struct.prog\data structured\problems in c++\SecondYear\Stack\Debug\Stack.exe

StuXYZ Nov 27th, 2008 5:32 am
Re: Stack with Dynamic Array
 
You have not defined a few components.
e.g.
you need to add a destructor
Stack::~Stack()
{
// stuff
}

etc.

Lotus_2011 Nov 27th, 2008 5:38 am
Re: Stack with Dynamic Array
 
but i defined it ...
here
Stack ::~Stack()
{
delete[]SS;
}
do you that or something else ???

StuXYZ Nov 27th, 2008 7:22 am
Re: Stack with Dynamic Array
 
Sorry my mistake, I just read the error message and assumed.
Ok so I copied and compiled the code.

Here is what I had to fix.

(a) Size in the constructor needs to be size.
(b) Expand doesn't have an SS_2 variable and the last line
should be
SS=SS_2;
(c) The guard in the assignment operator is mal formed
it should be
if (this!=&RHS)

(d) In the same function, SS_2 should read SS.
(e)
Stack::Push
capacity is actually size
(f) That is it...

Don't think the code works but it does compile.... time for you to test....


All times are GMT -4. The time now is 10:23 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC