|
-
May 7th, 2012, 11:32 AM
#1
left brace error '{'
hello can someone help me out to get this error done
PHP Code:
#include<iostream>
#include<assert.h>
#include<cstdio>
#include<cassert>
using namespace std;
template<class Type>
class stackType
{
public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize = 100);
stackType(const stackType<Type>& otherStack);
~stackType();
private:
int maxStackSize;
int stackTop; // variable to point to the top of the stack
Type *list;
void copyStack(const stackType<Type>& otherStack);
};
template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}
template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}
template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}
template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == 0);
}
template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;
stackTop++;
}
else
cout<<"Cannot add to a full stack."<<endl;
}
template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0);
return list[stackTop - 1];
}
template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
stackTop--;
else
cout<<"Cannot remove from an empty stack."<<endl;
}
template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
assert(list != NULL);
for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
}
template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cout<<"The size must be positive!"<<endl;
maxStackSize = 100;
}
else
maxStackSize = stackSize;
stackTop = 0;
list = new Type[maxStackSize];
assert(list != NULL);
}
template<class Type>
stackType<Type>::~stackType()
{
delete [] list;
}
template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;
copyStack(otherStack);
}
template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if(this != &otherStack)
copyStack(otherStack);
return *this;
template<class Type>
class queueType
{
public:
const queueType<Type>& operator=(const queueType<Type>&);
bool isEmptyQueue();
bool isFullQueue();
void initializeQueue();
void destroyQueue();
Type front();
Type back();
void addQueue(const Type& queueElement);
void deleteQueue();
queueType(int queueSize = 100);
queueType(const queueType<Type>& otherQueue);
~queueType();
private:
int maxQueueSize;
int count;
int queueFront;
int queueRear;
Type *list;
};
template<class Type>
const queueType<Type>& queueType<Type>:: operator=(const queueType<Type>& otherQueue)
{
if(this != &otherQueue)
{
delete [] list;
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueRear = otherQueue.queueRear;
queueFront = otherQueue.queueFront;
list = new Type[maxQueueSize];
assert(list != NULL);
for(int j = queueFront; j < count; j = (j+1)%maxQueueSize)
list[j] = otherQueue.list[j];
}
return *this;
}
template<class Type>
bool queueType<Type>::isEmptyQueue()
{
return (count == 0);
}
template<class Type>
bool queueType<Type>::isFullQueue()
{
return (count == maxQueueSize);
}
template<class Type>
void queueType<Type>::initializeQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
}
template<class Type>
void queueType<Type>::destroyQueue()
{
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
}
template<class Type>
Type queueType<Type>::front()
{
assert(!isEmptyQueue());
return list[queueFront];
}
template<class Type>
Type queueType<Type>::back()
{
assert(!isEmptyQueue());
return list[queueRear];
}
template<class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
if(!isFullQueue())
{
queueRear = (queueRear + 1) % maxQueueSize;
count++;
list[queueRear] = newElement;
}
else
cerr<<"Cannot add to a full queue."<<endl;
}
template<class Type>
void queueType<Type>::deleteQueue()
{
if(!isEmptyQueue())
{
count--;
queueFront = (queueFront + 1) % maxQueueSize;
}
else
cerr<<"Cannot remove from an empty queue."<<endl;
}
template<class Type>
queueType<Type>::queueType(int queueSize)
{
if(queueSize <= 0)
{
cerr<<"invalid size of array"<<endl;
maxQueueSize = 100;
}
else
maxQueueSize = queueSize;
queueFront = 0;
queueRear = maxQueueSize - 1;
count = 0;
list = new Type[maxQueueSize];
assert(list != NULL);
}
template<class Type>
queueType<Type>::queueType(const queueType<Type>& otherQueue)
{
delete [] list;
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueRear = otherQueue.queueRear;
queueFront = otherQueue.queueFront;
list = new Type[maxQueueSize];
for(int j = queueFront; j < count; j = (j+1)%maxQueueSize)
list[j] = otherQueue.list[j];
}
template<class Type>
queueType<Type>::~queueType()
{
delete [] list;
}
int main()
{
string word;
bool found=true;
cout<<"Enter a word ..\"stop\" to stop the program. "<<endl;
cin>>word;
while(word != "stop")
{
stackType<char> s1(word.length());
queueType<char> q1(word.length());
for(int i=0; i < word.length();i++)
{
s1.push(word[i]);
q1.addQueue(word[i]);
found=true;
}
while(!q1.isEmptyQueue()&& found)
{
if(q1.front()==s1.top())
{
q1.deleteQueue();
s1.pop();
}
else
found=false;
}
if (found){
cout<<"The word is PALINDROME."<<endl;
else
cout<<"The word is not PALINDROME."<<endl;
cout<<"Enter a word ..\"stop\" to stop the program. "<<endl;
cin>>word;}
return 0;}
-
May 7th, 2012, 11:44 AM
#2
Re: left brace error '{'
Code:
template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if(this != &otherStack)
copyStack(otherStack);
return *this;
}
-
May 7th, 2012, 11:48 AM
#3
Re: left brace error '{'
You also have some issues in main (at least it looks like it).
I recommend that you always use curly braces even if they are not needed. If you do that and also always make sure to keep a proper indentation this type of issue will be rare.
Edit: Sorry. Main is ok although it needs some editing.
Last edited by S_M_A; May 7th, 2012 at 12:03 PM.
-
May 7th, 2012, 12:46 PM
#4
Re: left brace error '{'
1) Please don't use PHP. There is no need to have code that looks like Christmas tree lights.
2) You should refrain from calling your items "list". There is already a "list" class in C++ (std::list), and naming your variables the same as existing classes may cause compilation issues.
3) Also, this is not quite correct:
Code:
if(this != &otherQueue)
{
delete [] list;
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueRear = otherQueue.queueRear;
queueFront = otherQueue.queueFront;
list = new Type[maxQueueSize]; // What if exception is thrown here?
So what would happen if "new[]" throws an exception? You've totally messed up your queueType object by setting and deleting things before you even know if calling new[] will succeed or not.
The fix is to call new[] first, and store the result in a temporary pointer. Then you rearrange/delete your items in the object.
Code:
Type *tempList = new Type[maxQueueSize];
delete [] list;
list = tempList;
maxQueueSize = otherQueue.maxQueueSize;
count = otherQueue.count;
queueRear = otherQueue.queueRear;
queueFront = otherQueue.queueFront;
for(int j = queueFront; j < count; j = (j+1)%maxQueueSize)
list[j] = otherQueue.list[j];
So now, the call to new[] is done first. If there is an exception thrown, your existing queueType object is not messed up.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|