compiler error

This is a discussion on compiler error within the C++ Programming forums, part of the General Programming Boards category; What is a parse error??...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    compiler error

    What is a parse error??

  2. #2
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    I've gotten parse errors when something was accidently added in or left out (may not be the greatest answer :/).

    I might be able to help if you paste the code that is giving you the error.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    code with parse error

    The errors are:

    26 G:\TestStack.cpp
    parse error before `{'

    32 G:\TestStack.cpp
    `e' undeclared (first use this function)


    (Each undeclared identifier is reported only once
    for each function it appears in.)
    At top level:


    111 G:\TestStack.cpp
    parse error before `return'

    G:\Makefile.win
    [Build Error] [TestStack.o] Error 1

    Here is the code:


    Code:
    #include <iostream>
    #include <cstdlib>
    #include "stackType.h"
    
    int main()
    {
    
    // declaring file stream variables
    
    ifstream inData;
    ofstream outData;
    
      int i;
      char *subject = "Data Structures";
      stack s;
      
    //Opening of files
    inData.open("a.stackinfo.txt");
    outData.open("a.stackoutput.txt");  
    
      cout<<subject[i];
      s.Push(subject[i]);
      ++i;
    
    
    cout<<"\nThe stack from the bottom to top:    ";
    //PrintStackUp(s);
    cout<<endl;
    
    // Function to print all elements in a stack and leave stack unchanged 
    // after printing
    //Pre: The stack is non empty
    //Post: 
    
    void PrintStack(stack& s)
    {
        el_t e;
        stack temp;
        
        while (!s.StackIsEmpty())
        {
            s.Pop(e);
            temp.Push(e);
            cout<<e;
        }
            while(!temp.StackIsEmpty())
            {
                temp.Pop(e);
                s.Push(e);
            }
    }
    
    void Print2nd(stack& s)
    {
        el_t e1, e2;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(e1);
            s.Pop(e2);
            cout<<e2;
            s.Push(e2);
            s.Push(e1);
        }
    }
    
    void PrintBottom(stack& s )
    {
        el_t e;
        stack temp;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(e);
            temp.Push(e);
        }
        cout<<e;
        while(!temp.StackIsEmpty())
        {
            temp.Pop(e);
            s.Push(e);
        }
    }
    
    
    void Palindrome(stack& s )
    {
        stack u,u2;
        el_t ch, ch1;
        bool equal = true;
        
        while(!s.StackIsEmpty())
        {
            s.Pop(ch);
            u.Push(ch);
            u2.Push(ch);
        }
            while(!u.StackIsEmpty())
            {
                u.Pop(ch);
                s.Push(ch);
            }
                while(!u2.StackIsEmpty() && equal)
                {
                     u2.Pop(ch);
                     s.Pop(ch1);
                       if (ch != ch1)
                           equal = false;
                }
                     if (equal)
                         cout<<"Palindrome"<<endl;
                              else
                                 cout<<"Not a Palindrome"<<endl;
    } //end of Palindrome Function
    
    
    
    
    
    //  system("PAUSE");	
      return 0;
    }
    //  File: stackType.h        Class, constant values, and type definitions for c++ static
    //  implementation of ADT stack.  This is the specification file for the class stack
    
    
    #ifndef STACK_H
    #define STACK_H
    
    // application dependent size of stack
    const unsigned int MAX_NUM_ELS = 256;
    
    typedef char el_t;       // type varies with application
    typedef int index_t;
    
    class stack
    {
        public:
            stack();                                //stack constructor
            ~stack();                               //stack deconstructor
            bool   StackIsEmpty(void);              //Is stack empty?
            bool   StackIsFull(void);               //Is stack full?
            void   Push(el_t);                      //push item
            void   Pop(el_t&);                      //pop to item
            
        private:
            void   StackError(char*);               //error handler
            index_t top;                            //top index: -1 if empty
            el_t   el[MAX_NUM_ELS];                 //container for elements
     };
     
     #endif

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You cannot declare local functions. You have to pull the functions out of main().

  5. #5
    eam
    eam is offline
    Registered User eam's Avatar
    Join Date
    Oct 2003
    Posts
    53
    The problem is you can't put your functions inside main(). Put them above main() and I think that should solve the problem.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    supect part of the problem may be in here, too, after you realign the functions as advised:

    char *subject = "Data Structures";
    stack s;

    //Opening of files
    inData.open("a.stackinfo.txt");
    outData.open("a.stackoutput.txt");

    cout<<subject[i];

    Often the compiler will highlight the line for you, then you can highlight the line for use, to make our task a little easier, should we attempt to help.

    The problems I see with the above snippet are two fold. First, I suspect your files aren't being opened, given the syntax of the path you are using. You should really check to see if the files opened or not to be sure. IF they aren't opened it's probably because you need to use some backslashes between the drive name and file name in the path, or any other subdirectories except between the actual file name and extension.

    Second, and more likely to be the cause of the first error, is that subject is declared as a char *. Unfortunately, char * don't know about the [] operator, so when you try to call it in the last line of the above snippet you get a "parse error" meaning there is a syntax error and the compiler can't parse the code into smaller pieces the way it should be able to.
    Last edited by elad; 11-05-2003 at 02:52 PM.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Thanks for all your help....

    Elad,


    I was still revising when I copied the code. I have since deleted the parts in question and fixed the input/output files.


    Eam & Lucky

    Thanks again, Ive been working on this for at least 2 days and I still need to study for a midterm.....

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    Second, and more likely to be the cause of the first error, is that subject is declared as a char *. Unfortunately, char * don't know about the [] operator, so when you try to call it in the last line of the above snippet you get a "parse error" meaning there is a syntax error and the compiler can't parse the code into smaller pieces the way it should be able to.
    Actually, using the [] operator is valid on a pointer.

    Code:
    char* str = "hello world";
    
    // these two statements are equivalent
    char a = *(str+6);
    char b = str[6];

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    [] is perfectly legal on a pointer. It has the following meaning:

    a[b] = *(a + b)

    This means:

    const char * str = "Hi there";
    return str[4];

    This would return 'h'.
    Also, 4[str] would return 'h' as well. Nobody uses this syntax except in convoluted code contests, I think.

    You can use this for lots of things. E.g. to randomly get a character representing the suit of a card:

    char suit = "SHDC"[rand()%4];
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I knew that there was a relationship between pointer notation and [] operator:

    char word[] = "Hi there";
    cout << word[4] << endl; //yields 'h';

    char * str = word;
    cout << *(str + 4) << endl;//yields 'h';

    But I didn't think you could use them interchangeably:
    cout << str[4] << endl;

    I presume
    cout << *(word + 4) << endl;//may yield 'h' as well

    I'll have to try it out at home.

    Thanks.
    Last edited by elad; 11-06-2003 at 01:49 PM.

  11. #11
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by elad
    I didn't think you could use them interchangeably:
    I presume
    cout << *(word + 4) << endl;//may yield 'h' as well
    Yup. In your example, "word" is the address of the start of a char array and the "+ 4" is the start address plus four bytes and the "*" dereferences the resultant address. It's essentially the same as:
    char *second = word;
    second += 4;
    cout << *second << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21