0

I defined two header files.

global.h

#ifndef GLOBAL_H
#define GLOBAL_H

#include <queue>
#include <string>
//#include "token.h"

class Token;

typedef std::string TokenValue;

enum TokenType{//...};

inline void clear(std::queue<Token> tokens)
{
    std::queue<Token> empty;

    std::swap(tokens, empty);
}

#endif // GLOBAL_H

and token.h

#ifndef TOKEN_H
#define TOKEN_H

#include "global.h"

class Token
{
public:
    Token (TokenType token_type, TokenValue token_value)
    {
        token_type_ = token_type;
        token_value_ = token_value;
    }

    ~Token (){}

//...

private:
    TokenType token_type_;
    TokenValue token_value_;
};

I use foward declaration in global.h, but i don't use the reference or pointer of class Token. I use std::queue<Token> empty; in global.h. I think this statement must need the size of Token. I can't figure out why it can compiles success. It's the problem of queue?

stamaimer
  • 6,227
  • 5
  • 34
  • 55
  • 3
    You're basically asking "Why does this compile when I use a C++ standard container with an incomplete type?". Here, the standard container you're using is `std::queue`, and `Token` is the incomplete type. [The C++ *standard* technically does not allow this](http://stackoverflow.com/questions/18672135/why-c-containers-dont-allow-incomplete-types), but it may just happen to work because the internal container *implementation* does not need the complete type. This may or may not be true with other C++ compilers. – In silico Nov 24 '13 at 09:44
  • You are right. The first compiler i use is gcc. The compiler of microsoft pointed this error.thanks a lot.@In silico – stamaimer Nov 24 '13 at 10:24

1 Answers1

-2

When you include global.hin token.h the compiler has the complete information to work. Try to include global.h in another file and you will have your compile error :-)

Tio Pepe
  • 3,071
  • 1
  • 17
  • 22
  • 2
    This is not the case. The definition for `Token` is provided *after* its use. The compiler does not have the complete definition for `Token` by the time `std::queue empty` is instantiated. – In silico Nov 24 '13 at 09:47