Reputation: 519
I am writing a parser for a compiler . So for the constructor I have the code :
//constructor
Parser::Parser(char* file)
{
MyLex(file) ;
}
Upon compiling using g++ parsy.cpp parsydriver.cpp , I am however getting this error saying :
parsy.cpp: In constructor ‘Parser::Parser(char*)’:
parsy.cpp:13: error: no matching function for call to ‘Lex::Lex()’
lexy2.h:34: note: candidates are: Lex::Lex(char*)
lexy2.h:31: note: Lex::Lex(const Lex&)
parsy.cpp:15: error: no match for call to ‘(Lex) (char*&)’
Where am I going wrong ? Lex myLex is declared as private in the Parser header . I am at my wit's end . I tried using this :
//constructor
Parser::Parser(char* file):myLex(file)
{
}
My Lexical Analyser constructor is :
Lex::Lex(char* filename): ch(0)
{
//Set up the list of reserved words
reswords[begint] = "BEGIN";
reswords[programt] = "PROGRAM";
reswords[constt] = "CONST";
reswords[vart] = "VAR";
reswords[proceduret] = "PROCEDURE";
reswords[ift] = "IF";
reswords[whilet] = "WHILE";
reswords[thent] = "THEN";
reswords[elset] = "ELSE";
reswords[realt] = "REAL";
reswords[integert] = "INTEGER";
reswords[chart] = "CHAR";
reswords[arrayt] = "ARRAY";
reswords[endt] = "END";
//Open the file for reading
file.open(filename);
}
but, this creates a bunch of undefined reference to Lexical Analyser file and functions ! I have included the files properly. But so far , I don't understand how to get over this problem.
UPDATE The header file inclusions are :
parsy.h file :
#ifndef PARSER_H
#define PARSER_H
// other library file includes
#include "lexy2.h"
class Parser
{
}...
parsy.cpp file :
// usual ilbraries
#include "parsy.h"
using namespace std ;
Parser::Parser(char* file) ....
parsydriver.cpp :
// usual libraries
#include "parsy.h"
using namespace std ;
int main()
..
lexy2.cpp file :
I have included the lexy2.h file. Should I be including the parser header files in the lexical analyser ones ? Doesn't seem likely. But how should I tackle them then ?
Upvotes: 0
Views: 161
Reputation: 647
Code inside constructor runs when object is already constructed. Your class MyLex
does not have default constructor. So you have to define default constructor or it should be:
//constructor
Parser::Parser(char* file): MyLex(file)
{
}
If you have "undefined symbol" linker errors then you forgot to add some .cpp
files (maybe lexy2.cpp) to the project or compiler command line. Assuming that all undefined symbols located in lexy2.cpp then try g++ parsy.cpp parsydriver.cpp lexy2.cpp
.
Upvotes: 2