Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I keep getting this error message and the code below:
I can't figure out why any help would be appreciated.
Line 51 is this statement in my code:
Element* Stack::Element::Prev(void)
error message:
In file included from Stack03.cpp:28:
Stack03.h:81:7: warning: no newline at end of file
Stack03.cpp:51: syntax error before `*' token
Stack03.cpp:127:87: warning: no newline at end of file
make: *** [Stack03.o] Error 1 #include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________
using namespace std;
//____________________________________________________________________________________
#include "Stack03.h"
CommandLineException::CommandLineException(int max, int actual)
{
cout << " Too many command line arguments. " << endl;
}
//____________________________________________________________________________________
FileException::FileException(char* fn)
{
cout << " File " << fn << " could not be opened. " << endl;
}
//____________________________________________________________________________________
Stack::Element::Element(Element *p, int v)
{
prev = p; // initialization of variables of class Element.
value = v; // initialization of variables of class Element.
}
//____________________________________________________________________________________
Element* Stack::Element::Prev(void)
{
return prev;
}
//____________________________________________________________________________________
int Stack::Element::Value(void)
{
return value;
}
//____________________________________________________________________________________
//____________________________________________________________________________________
Stack::StackException::StackException(char* m)
{
cout << endl;
cout << " I am the stack and I am " << m << " . " << endl;
}
//____________________________________________________________________________________
Stack::Stack()
{
top_of_stack = 0; // initialization of variables of class Stack.
}
//____________________________________________________________________________________
Stack::~Stack()
{
Kill(top_of_stack);
}
//____________________________________________________________________________________
void Stack::Kill(Element* e)
{
while(e)
{
Element* p = e;
e = e->Prev();
delete []p;
} // end of while loop.
}
//____________________________________________________________________________________
bool Stack::Is_Full(void)
{
return false;
}
//____________________________________________________________________________________
bool Stack::Is_Empty(void)
{
return top_of_stack == 0;
}
//____________________________________________________________________________________
void Stack::Push(int v)
{
if (Is_Full())
throw StackException("Full");
Element* n = new Element(top_of_stack,v);
top_of_stack = n;
}
//____________________________________________________________________________________
int Stack::Pop(void)
{
if (Is_Empty())
throw StackException("Empty");
Element* n = top_of_stack;
int v=n->Value();
top_of_stack = n;
delete n;
return v;
} #ifndef STACK_H
#define STACK_H
#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________
using namespace std;
class CommandLineException
{
public:
CommandLineException(int max, int actual);
};
//____________________________________________________________________________________
class FileException
{
public:
FileException(char* fn);
};
//____________________________________________________________________________________
class Stack
{
class Element
{
private:
Element* prev;
int value;
public:
Element(Element* p, int v); // member function of class Element
Element* Prev(void); // member function of class Element
int Value(void); // member function of class Element
}; // end of the class Element
//____________________________________________________________________________________
class StackException
{
public:
StackException(char* m); // constructor of class StackException
}; // end of class StackException
private:
Element* top_of_stack;
void Kill(Element* e); // member function of class Stack
public:
Stack();
~Stack();
bool Is_Full(void);
bool Is_Empty(void);
void Push(int v);
int Pop(void);
}; // end of class Stack
#endif
main code:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
//____________________________________________________________________________________
using namespace std;
//____________________________________________________________________________________
#include "Stack03.h"
#include "Scan03.h"
//____________________________________________________________________________________
void PostfixMgr(FILE* i,ostream& o);
//____________________________________________________________________________________
int main(int argc, char* argv[])
{
try
{
char infile[255]; // input file
char outfile[255]; // output file
if (argc == 1)
{
cout << " Enter the input file name. " << endl;
cin >> infile;
cout << " Enter the output file name. " << endl;
cin >> outfile;
cout << endl;
}
else if (argc == 2)
{
strcpy(infile, argv[1]);
cout << " Enter the output file name. " << endl;
cin >> outfile;
cout << endl;
}
else if ( argc == 3)
{
strcpy(infile, argv[1]);
strcpy(outfile, argv[2]);
}
else
{
throw CommandLineException(2,argc -1);
}
FILE* i= fopen(infile,"r");
if(!i)
throw FileException(infile);
ofstream o(outfile);
if(!o)
throw FileException(outfile);
PostfixMgr(i,o);
fclose(i);
o.close();
} catch(...)
{
cout << " Program Terminated. " << endl;
exit(EXIT_FAILURE);
}
return 0;
} // end of main.
void PostfixMgr(FILE* i,ostream& o)
{
Scan L(i);
Stack s;
int k = 0;
int front=0;
int back = 0;
int divisor =0;
int div = 0;
int answer = 0;
int counter = 0;
for(;;)
{
int t=L.Lex();
if(t==0)
break;
cout << endl;
cout << setw(4) << t;
cout << setw(30) << L.FetchSpelling();
switch (t)
{
case INTLIT:
sscanf(L.FetchSpelling(),"%d",&k);
s.Push(k);
break;
case PLUS:
s.Push(s.Pop() + s.Pop());
break;
case MINUS:
back = s.Pop();
front = s.Pop();
s.Push(front - back);
break;
case SLASH:
div = s.Pop();
divisor = s.Pop();
s.Push(divisor/div);
break;
case STAR:
s.Push(s.Pop() + s.Pop());
break;
default:
cout << " Did not recognize token" << endl;
cout << endl;
break;
} // end of switch statement.
while ( !s.Is_Empty())
{
cout << "S[top_of_stack]=" << " " << s.Pop();
cout << endl;
counter++;
} // end of while loop.
} // end of for loop.
} // end of sfixMgr>Element* Stack::Element::Prev(void)
How about:
Stack::Element* Stack::Element::Prev(void)Thanks that fixed it but I am not sure why. Because I thought that Element* was the return type and that had to be stated first before the class Stack. Oh well. Now I have an infinite loop to deal with.
thanks again.
>Thanks that fixed it but I am not sure why.
Element is declared inside Stack, so you have to qualify which Element you're talking about or the compiler won't be able to find it. Just like if you're using cout but forget to qualify it with std:: somehow.