CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    Talking 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 TypenewItem);
        
    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 TypenewItem)
    {
        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 0stackTopj++)
            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 TypequeueElement);
        
    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 queueFrontcount= (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 TypenewElement)
    {
        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 queueFrontcount= (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<chars1(word.length());
        
    queueType<charq1(word.length());

        for(
    int i=0word.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;} 

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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; 
    }
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    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
  •  





Click Here to Expand Forum to Full Width

Featured