'Scanner'没有在g ++中命名类型错误 [英] 'Scanner' does not name a type error in g++

查看:167
本文介绍了'Scanner'没有在g ++中命名类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在g ++中编译代码,并得到以下错误:



在scanner.hpp包含的文件中:8,

从scanner.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>
#includescanner.hpp
using std :: string;


类解析器
{
//成员变量
private:
Scanner lex; //词法分析器
令牌看//跟踪当前的前瞻标记

//成员函数
< some function declarations>
};

#endif

这里是scanner.hpp:

  #ifndef SCANNER_HPP 
#define SCANNER_HPP

#include< iostream>
#include< cctype>
#include< string>
#include< map>
#includeparser.hpp
using std :: string;
使用std :: map;

enum
{
//保留字
BOOL,ELSE,IF,TRUE,WHILE,DO,FALSE,INT,VOID,
标点和运算符
LPAREN,RPAREN,LBRACK,RBRACK,LBRACE,RBRACE,SEMI,COM​​MA,PLUS,MINUS,TIMES,
DIV,MOD,AND,OR,NOT,IS,ADDR,EQ,NE ,LT,GT,LE,GE,
//符号常量
NUM,ID,ENDFILE,ERROR
};


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

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

类Word:public Token
{
public:
Word(){ta​​g = 0; lexeme =default;}
Word(int t,string l){tag = t; lexeme = l;}
};


类扫描器
{
private:
int line; //编译器当前所在的行
int depth; //在解析树中有多深,编译器是
map< string,Word>话; //保留字列表和使用的标识符

//成员函数
public:
Scanner();
Token scan();
string printTag(int);
friend class Parser;
};

#endif

任何人都看到问题?我觉得我缺少一些非常明显的东西。

解决方案

parser.hpp incluser scanner.hpp,反之亦然。



所以一个文件在另一个文件之前。



您可以使用

  

或重新标记标题


I'm trying to compile code in g++ and I get the following errors:

In file included from scanner.hpp:8,
from scanner.cpp:5:
parser.hpp:14: error: ‘Scanner’ does not name a type
parser.hpp:15: error: ‘Token’ does not name a type

Here's my g++ command:

g++ parser.cpp scanner.cpp -Wall

Here's 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

and here's 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

anyone see the problem? I feel like I'm missing something incredibly obvious.

解决方案

parser.hpp incluser scanner.hpp and vice versa.

So one file evalated before the other.

You can use a forward declaration like

class Scanner;

or reorginaze your headers

这篇关于'Scanner'没有在g ++中命名类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆