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

Recommended Answers

All 3 Replies

You have not defined a few components.
e.g.
you need to add a destructor
Stack::~Stack()
{
// stuff
}

etc.

but i defined it ..
here

Stack ::~Stack()
{
    delete[]SS;
}

do you that or something else ???

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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.