2014-08-30 2 views
0

저는 C++을 처음 사용하기 때문에이 클래스는 플렉스 스캐너와 함께 사용됩니다. 난 그냥 컴파일을 얻기 위해 여기에 간단 유지하고있어하지만 다음과 같은 메시지를 받고 없을거야 (이 메시지에 대한 다른 스레드의 아무도 나의 상황에 적용하는 것) : 아키텍처 x86_64에 대한C++을 처음 사용하는 경우

정의되지 않은 심볼 : "Listing :: lexicalError",에서 참조 된 : Listing : Listing() in listing.o Listing : displayErrorCount() in listing.o listing :: increaseLexicalError() in listing.o ld : 기호 (들) 아키텍처에 대해 찾을 수 없음 x86_64

Listing.

어떤 도움의 컴파일에 대한
using namespace std; 

class Listing 
{ 

public: 
    enum ErrorType {LEXICAL, SYNTAX, SEMANTIC}; 

Listing(); 

void appendError(ErrorType error, char yytext[]); 

void displayErrorCount(); 

void increaseLexicalError(); 

private: 

static int lexicalError; 

}; 

Listing.cpp는

#include <iostream> 
#include <sstream> 
using namespace std; 

#include "Listing.h" 


Listing::Listing() 
{ 
    lexicalError = 0; 
} 

void Listing::appendError(ErrorType error, char yytext[]) 
{ 
    switch (error) { 
     case LEXICAL: 
      cout << "Lexical Error, Invalid Character " << yytext << endl; 
      break; 
     case SEMANTIC: 
      cout << "Semantic Error, "; 
     case SYNTAX: 
      cout << "Syntax Error, "; 

    default: 
     break; 
} 
} 

void Listing::displayErrorCount() 
{ 
    cout << "Lexical Errors " << lexicalError << " "; 

} 

void Listing::increaseLexicalError() 
{ 
    lexicalError++; 
} 

감사합니다.

compile: scanner.o listing.o 
    g++ -o compile scanner.o listing.o 

scanner.o: scanner.c listing.h tokens.h 
    g++ -c scanner.c 

scanner.c: scanner.l 
    flex scanner.l 
    mv lex.yy.c scanner.c 

listing.o: listing.cpp listing.h 
    g++ -c listing.cpp 
+0

우리에게 당신의 파일을 보여주세요. – Jay

+0

정적 변수가 개인이 될 수 있다고 생각하지 않습니다.하지만해야합니까? 어쨌든 정적을 제거하면 빌드 프로세스가 작동하는 것처럼 보입니다. 그리고 makefile은 필요하지 않습니다. VS에서 동일한 오류가 발생합니다. – Gizmo

+0

[정의되지 않은 참조/확인되지 않은 외부 기호 오류 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external- symbol-error-and-do-do-i-fix) –

답변

4

당신은 당신의 .cpp 파일에서 정적 멤버 변수 lexicalError을 정의 할 수 있습니다 : 여기

은 메이크의 ...는 C++ 코드가 없습니다 꽤하지만 난 배우고 확신

#include <iostream> 
#include <sstream> 
using namespace std; 

#include "Listing.h" 

// here is the definition 
int Listing::lexicalError = 0; 

Listing::Listing() 
{ 
    // not sure if you really want to do this, it sets lexicalError to zero 
    // every time a object of class Listing is constructed 
    lexicalError = 0; 
} 

[...] 
+0

그것을 한 암소! – MayNotBe

관련 문제