2014-08-30 112 views
0

我是新來的C++,這個類將與flex掃描器一起使用。我是來這裏簡單只是爲了得到它來編譯,但我得到了以下消息(無有關此消息的其他線程似乎適用於我的情況):用於建築x86_64的麻煩編譯 - 新的C++

未定義的符號: 「Listing :: lexicalError」,引用來自: Listing.o中的列表:: Listing() 列表:: displayErrorCount()在listing.o中 列表:: increaseLexicalError()在列表中.o ld:symbol(s)未找到適用於架構的x86_64

列表.h

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++; 
} 

感謝您的幫助編譯。我敢肯定的C++代碼是不漂亮,但我學習......

這裏的Makefile文件:

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

向我們展示您的make文件 – Jay 2014-08-30 17:44:22

+0

我不認爲一個靜態變量可以是私人的..但它應該?無論如何去除靜態似乎使構建過程的工作。並且makefile不是必需的。在VS上發生同樣的錯誤。 – Gizmo 2014-08-30 17:55:45

+0

可能重複[什麼是未定義的引用/未解析的外部符號錯誤,以及如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external- symbol-error-and-how-do-i-fix) – 2014-08-30 17:58:44

回答

4

您的.cpp文件中定義你的靜態成員變量lexicalError

#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 2014-08-30 18:00:08