gpt4 book ai didi

c++ - 编译麻烦 - c++ 新手

转载 作者:行者123 更新时间:2023-11-27 23:03:23 25 4
gpt4 key购买 nike

我是 c++ 的新手,这个类将与 flex 扫描仪一起使用。我在这里保持简单只是为了让它编译但我收到以下消息(关于此消息的其他线程似乎都不适用于我的情况):

Undefined symbols for architecture x86_64: "Listing::lexicalError", referenced from: Listing::Listing() in listing.o Listing::displayErrorCount() in listing.o Listing::increaseLexicalError() in listing.o ld: symbol(s) not found for architecture x86_64

list .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;

};

list .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++ 代码不漂亮,但我正在学习 ...

这是生成文件:

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

最佳答案

您必须在 .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;
}

[...]

关于c++ - 编译麻烦 - c++ 新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25585139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com