tags:

views:

103

answers:

3

Hi. 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.

A: 

You have circular #include reference: one header file includes another and vice versa. You need to break this loop somehow.

doublep
+1  A: 

You are including Scanner.hpp in Parser.hpp and you are also including Parser.hpp in Scanner.hpp.

If you include Scanner.hpp in your source file then the definition of the Parser class will appear before the definition of the Scanner class and you will get the error you are seeing.

Resolve the circular dependency and your problem will go away (headers should never circularly depend on each other for types).

James McNellis
+1  A: 

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

plaisthos
Ah, right. I knew it was something simple. Thanks, everyone.
Max