Note that there are some explanatory texts on larger screens.

plurals
  1. POdeclaring class variable c++
    primarykey
    data
    text
    <p>So I'm at a complete loss...</p> <p>In my code I've got</p> <pre><code>void Parser(FILE* file) { Parser par(file); par.Parse(); } </code></pre> <p>and I call it in my main function with <code>Parser(file);</code> and the header file I've got (which I included in the main file) looks like:</p> <pre><code>class Parser: public Lexer { public: Parser(FILE* file):Lexer(file); int Parse(); }; </code></pre> <p>and the error I'm getting is:</p> <pre><code>p02.cpp: In function 'void Parser(FILE*)': p02.cpp:20: error: expected ';' before 'par' p02.cpp:21: error: 'par' was not declared in this scope make: *** [p02.o] Error 1 </code></pre> <p>What I don't understand is why it is expecting a semicolon before par. Isn't that a legal declaration of a variable for that class?</p> <p>Edit2: Changing my function name to not be Parser like the class name does not solve this problem. It does give me an <em>extra</em> error telling me that Parser is not declared in this scope, but I cannot see how that is when I've added the include file containing the Parser class right above the declaration for the function.</p> <p>Edit: My Files p02.cpp:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;iostream&gt; #include &lt;cstring&gt; #include &lt;string&gt; #include "p02lex.h" #include "y.tab.h" using namespace std; void Parser(FILE* file) { Parser par(file); par.Parse(); } int main(int argc, char* argv[]) { char fileName[255]; switch(argc) { case 1: cout &lt;&lt; "Enter the input file name. "; cin &gt;&gt; fileName; break; case 2: strcpy(fileName, argv[1]); break; default: cout &lt;&lt; "Too many arguments!\n"; exit(1); } FILE* file = fopen(fileName, "r"); Parser(file); fclose(file); return 0; } </code></pre> <p>p02lex.l:</p> <pre><code>#include "p02lex.h" #define ID 257 ... #define PROGRAM 304 int TokenMgr(int t); const char* getTokens(int tokenCode); unsigned lineCount = 1, columnCount = 1; %} LETTER [a-z]|[A-Z] DIGIT [0-9] %% // rules defined here, calling TokenMgr() %% int TokenMgr(int t) { /* int tc = t; if (t == IDENTIFIER) { char s[1024]; ToLower(s, yytext, strlen(yytext)); tc = RW[s]; if (tc == 0) tc = t; } PrintToken(tfs, tc, line, col); col += yyleng; */ //JEG printf("Token:Code=%d Name=%10s line=%3u col=%3u Spelling=\"%s\"\n", t, getTokens(t), lineCount, columnCount, yytext); columnCount += yyleng; return /* tc */ 0; // JEG } Lexer::Lexer(FILE* file) { yyin = file; } int Lexer::Scan(void) { return yylex(); } const char* getTokens(int tokenCode) { switch(tokenCode) { case ID: return "ID"; ... // more cases, returning strings default: return NULL; } } </code></pre> <p>p02lex.h:</p> <pre><code>#ifndef p02lex_h #define p02lex_h 1 #endif int yylex(void); class Lexer { public: Lexer(FILE* file); int Scan(void); }; </code></pre> <p>p02par.h:</p> <pre><code>#ifndef p02par_h #define p02par_h 1 #endif using namespace std; #ifdef __cplusplus extern "C" #endif int yyparse(void); class Parser: public Lexer { public: Parser(FILE* file):Lexer(file){} void Parse(); // int Scan(void); }; </code></pre> <p>p02par.y:</p> <pre><code>#include &lt;stdio.h&gt; #include "p02lex.h" #include "p02par.h" void yyerror(const char* m); %} %token PROGRAM %token ID %token SEMICOLON %% program: PROGRAM ID SEMICOLON { printf("Stuff happens!\n"); } %% void yyerror(const char* m) { printf("%s\n", m); } /*Parser::Parser(FILE* file):Lexer(file) { }*/ int Parser::Parse() { return yyparse(); } </code></pre> <p>p02make:</p> <pre><code>#LEX = flex #YACC = yacc -d CC = g++ OBJ = p02.o p02par.o p02lex.o p02: ${OBJ} $(CC) -o p02 ${OBJ} -ll -ly y.tab.h p02par.cpp: p02par.y yacc -d -v p02par.y mv y.tab.c p02par.cpp p02lex.cpp: p02lex.l lex p02lex.l mv lex.yy.c p02lex.cpp p02par.o: p02par.cpp p02par.h $(CC) -c -g p02par.cpp p02.o: p02.cpp p02lex.h p02par.h $(CC) -c -g p02.cpp p02lex.o: p02lex.cpp p02lex.h y.tab.h $(CC) -c -g p02lex.cpp </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload