2014-08-30 104 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