我是c ++的新手,这个类将与flex扫描仪一起使用。我在这里保持简单只是为了让它编译,但我得到以下消息(关于此消息的其他线程似乎都不适用于我的情况):
架构x86_64的未定义符号: " Listing :: lexicalError",引自: 清单:: listing()中的listing() 在listing.o中列出:: displayErrorCount() 在listing.o中列出:: increaseLexicalError() ld:找不到架构x86_64的符号
Listing.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 :(得分: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;
}
[...]