What is a parse error??
Printable View
What is a parse error??
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.
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
You cannot declare local functions. You have to pull the functions out of main().
The problem is you can't put your functions inside main(). Put them above main() and I think that should solve the problem.
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.
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.....
Actually, using the [] operator is valid on a pointer.Quote:
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.
Code:char* str = "hello world";
// these two statements are equivalent
char a = *(str+6);
char b = str[6];
[] 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];
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.
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:Quote:
Originally posted by elad
I didn't think you could use them interchangeably:
I presume
cout << *(word + 4) << endl;//may yield 'h' as well
char *second = word;
second += 4;
cout << *second << endl;