0

IS their a way I can declare two classes that depend on each other? I tried to see if you could forward declare classes and their members but I didn't see anything that would help. Here is the code

class lexer_c {
public:
    std::string src;            // Src, a buffer that stores a string copy of the input file.
    size_t line;                // Line, holds the current line the parser is reading from.
    size_t pos;                 // Pos, the position on the line the parser is reading at.
    size_t ptr;                 // Ptr, the current index in the input buffer.
    size_t len;                 // Len, the length of the input buffer.

    lexer_c(std::string file_name);
    token_c lexer_c::next_token(); // DEPENDS ON TOKEN_C BELOW
};

class token_c {
public:
    tk_type type;               // Type of the token.
    size_t line;                // The line number the token was recorded at.
    size_t pos;                 // The position of the start of the token.

    union {                     // Union:
        char* str;              //  String, for tokens that need to store a string value.
        double flt;             //  Flt, for tokens(_flt_rep) that will store a float value.
        unsigned int num;       //  Interger, for tokens(_int_rep) that need to store integer types.
    };

    token_c(lexer_c* lexer, tk_type t); // DEPENDS ON LEXER_C AOVE
    void token_c::print(token_c token);
};
Hedron
  • 86
  • 1
  • 9
  • 1
    This looks like what you want: http://stackoverflow.com/questions/625799/resolve-header-include-circular-dependencies-in-c – NathanOliver Aug 08 '16 at 17:34
  • Yes, you just need to forward-declare the class. Put this above `lexer_c`: `class token_c;` – zneak Aug 08 '16 at 17:34
  • @zneak No that wouldn't work because the function next_token() return token_c by value, for which the full definition is required. – MatrixAndrew Aug 08 '16 at 17:36
  • 2
    @MatrixAndrew, you can declare a function that returns any type by value even if its definition is incomplete. The type definition needs to be complete only when you implement the function or when you call it. See http://www.cpp.sh/2ze7g – zneak Aug 08 '16 at 17:39
  • 1
    @zneak Wow, sorry that I called you out wrongly on that then, I didn't know that – MatrixAndrew Aug 08 '16 at 17:42
  • are you familiar with the "friend" keyword in C++? – Kyle Sweet Aug 08 '16 at 19:20

0 Answers0