2013-02-26 2 views
0

컴파일러 용 파서를 작성하고 있습니다.C++ 생성자 오류

//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); 
} 

하지만, 이것은 어휘 분석기 파일과 기능에 대한 정의되지 않은 참조의 무리를 만듭니다! 파일을 올바르게 포함 시켰습니다. 그러나 지금까지이 문제를 극복하는 방법을 이해하지 못했습니다.

UPDATE 헤더 파일 흠은 다음과 같습니다

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 파일을 포함 시켰습니다. 어휘 분석기에 파서 헤더 파일을 포함시켜야합니까? 가능성이없는 것 같습니다. 그런데 어떻게 내가 그걸 다루어야합니까?

+3

두 번째 방법은 적절한 방법입니다. 문제는 처음 일한 후에도 여전히 존재할 것입니다. 그것들은 어떤 형태로든 올바르게 링크되지 않은 링커 오류입니다. – chris

+0

어떻게 링커 오류를 극복 할 수 있습니까? 나는 헤더 파일을 포함시키는 질문을 편집하고 업데이트 할 것이다. – thestralFeather7

+0

선택하십시오 : http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – chris

답변

2

생성자 내부의 코드는 객체가 이미 생성되었을 때 실행됩니다. 클래스 MyLex에 기본 생성자가 없습니다. 그래서 당신은 기본 생성자를 정의 할 필요가 있거나해야한다 :

//constructor 
Parser::Parser(char* file): MyLex(file) 
{ 
} 

이있는 경우

"정의되지 않은 기호"링커 오류는 다음 프로젝트 나 컴파일러 명령 줄에 일부 .cpp 파일 (아마도 lexy2.cpp)를 추가하는 것을 잊었다. lexy2.cpp에있는 모든 정의되지 않은 기호가 있다고 가정하면 g++ parsy.cpp parsydriver.cpp lexy2.cpp을 시도하십시오.

+0

오케이 그래서 4 파일 +1 드라이버 파일이 있습니다. 쉘에서 어떻게 컴파일해야합니까? 파일은 다음과 같습니다. lexy2.h lexy2.cpp parsy.h parsy.cpp 및 parsydriver.cpp – thestralFeather7

+0

@ novice7 수정 된 답변은 명령 줄 예제를 추가합니다. – Sergey