gpt4 book ai didi

c++ - 'Scanner' 没有命名 g++ 中的类型错误

转载 作者:行者123 更新时间:2023-11-30 02:11:51 26 4
gpt4 key购买 nike

我正在尝试用 g++ 编译代码,但出现以下错误:

在 scanner.hpp:8 包含的文件中,
来自扫描仪.cpp:5:
parser.hpp:14: 错误:“扫描仪”没有命名类型
parser.hpp:15: 错误:'Token' 没有命名类型

这是我的 g++ 命令:

g++ parser.cpp scanner.cpp -Wall

这是 parser.hpp:

#ifndef PARSER_HPP
#define PARSER_HPP
#include <string>
#include <map>
#include "scanner.hpp"
using std::string;


class Parser
{
// Member Variables
private:
Scanner lex; // Lexical analyzer
Token look; // tracks the current lookahead token

// Member Functions
<some function declarations>
};

#endif

这是 scanner.hpp:

#ifndef SCANNER_HPP
#define SCANNER_HPP

#include <iostream>
#include <cctype>
#include <string>
#include <map>
#include "parser.hpp"
using std::string;
using std::map;

enum
{
// reserved words
BOOL, ELSE, IF, TRUE, WHILE, DO, FALSE, INT, VOID,
// punctuation and operators
LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, SEMI, COMMA, PLUS, MINUS, TIMES,
DIV, MOD, AND, OR, NOT, IS, ADDR, EQ, NE, LT, GT, LE, GE,
// symbolic constants
NUM, ID, ENDFILE, ERROR
};


class Token
{
public:
int tag;
int value;
string lexeme;
Token() {tag = 0;}
Token(int t) {tag = t;}
};

class Num : public Token
{
public:
Num(int v) {tag = NUM; value = v;}
};

class Word : public Token
{
public:
Word() {tag = 0; lexeme = "default";}
Word(int t, string l) {tag = t; lexeme = l;}
};


class Scanner
{
private:
int line; // which line the compiler is currently on
int depth; // how deep in the parse tree the compiler is
map<string,Word> words; // list of reserved words and used identifiers

// Member Functions
public:
Scanner();
Token scan();
string printTag(int);
friend class Parser;
};

#endif

有人看到问题了吗?我觉得我错过了一些非常明显的东西。

最佳答案

parser.hpp 包括用户 scanner.hpp,反之亦然。

所以一个文件先于另一个文件被评估。

你可以使用像

这样的前向声明
class Scanner;

或者重新组织你的标题

关于c++ - 'Scanner' 没有命名 g++ 中的类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706040/

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