コンパイラ用のパーサーを書いています。したがって、コンストラクターのコードは次のとおりです。
//constructor
Parser::Parser(char* file)
{
MyLex(file) ;
}
ただし、 g++ parsy.cpp parsydriver.cppを使用してコンパイルすると、次のエラーが表示されます。
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*&)’
どこが間違っていますか?Lex myLexは、パーサー ヘッダーでプライベートとして宣言されています。私は途方に暮れています。私はこれを使ってみました:
//constructor
Parser::Parser(char* file):myLex(file)
{
}
私の語彙アナライザーのコンストラクターは次のとおりです。
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);
}
しかし、これにより、Lexical Analyzer ファイルと関数への未定義の参照が大量に作成されます。ファイルを適切に含めました。しかし、これまでのところ、この問題を克服する方法がわかりません。
更新 ヘッダーファイルのインクルードは次のとおりです。
parsy.h ファイル:
#ifndef PARSER_H
#define PARSER_H
// other library file includes
#include "lexy2.h"
class Parser
{
}...
parsy.cpp ファイル:
// 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 ファイル:
lexy2.h ファイルを含めました。パーサーヘッダーファイルを字句解析器に含める必要がありますか? ありそうにない。しかし、どうすればそれらに取り組むことができますか?