Use of undeclared identifier 'Token'

699 Views Asked by At

I was writing an interpreter in C, but the complexity was too high. I started put the code into cpp classes and I get an error:

#ifdef _TOKEN_H
#define _TOKEN_H
enum TOKEN_TYPE {INTEGER, IDENTIFIER, KEYWORD, OPERATOR, UNKNOWN};

class Token
{
  public:
    Token(string v = "", TOKEN_TYPE t = UNKNOWN);

  private:
    string value;
    TOKEN_TYPE type;
};

#endif

And the Token.cpp

#include "Token.h"
using namespace std;

Token::Token(string v, TOKEN_TYPE t)
{
   value = v;
   type = t;
}

Use of undeclared identifier 'Token'

Anyone can help me?

2

There are 2 best solutions below

0
On

Your header guard is incorrect. It should read:

#ifndef _TOKEN_H
// ^

Also, tokens beginning with an underscore and capital letter are reserved for any use by the implementation. So it should be:

#ifndef TOKEN_H

Or even #ifndef TOKEN_H_GUARD

0
On

Two things:

  1. #ifdef _TOKEN_H needs to be #ifndef _TOKEN_H. (Also avoid leading underscores followed by capital letters as, technically, the behaviour of the program will be undefined).

  2. Use std::string in place of string in your header, as you're including the header before using namespace std;

Writing using namespace std; in a header can case problems with namespace pollution so what you're currently doing is preferable.