0
#include "Graph.h"
#include "Graphics.h"
#include "MainWindow.h"
#include "SnakeGoal.h"
typedef struct {
    std::pair<double, double> coordinates; //x, y
    int direction;
}directionChange;

class Snake {
private:
    std::pair<double, double> coordinates;
    std::deque<directionChange> direcChanges; //a deque of direction 
    std::deque<Snake> body; //the body of the snake
    int direction;

The deque objects are causing a chain reaction of errors. It says

use of undefined type 'Snake' (compiling source file Snake.cpp)

Am i not allowed to have a deque object of my class type here? If not, what is the right way to do this? It is working just fine on onlinegdb.ocom

  • 4
    It contains itself? Some serious ouroboros code here. – tadman May 21 '20 at 17:36
  • What sort of snake has a collection of other snakes for a body? – Asteroids With Wings May 21 '20 at 17:42
  • 2
    Unfortunately, inside type declareation the type is not yet fully declared, so you cannot use it for e.g. specyfying templates (like you are trying to do with `std::deque`). But I don't think this is what you want anyway, you need some `SnakeSegment` class to represent body and then `Snake` will bind them together in an `std::deque`. – Yksisarvinen May 21 '20 at 17:42
  • 1
    How do you expect the compiler to know what a `Snake` is in code that's part of defining `Snake`? – Jesper Juhl May 21 '20 at 17:43
  • Though it's 18 years old, this article provides some good background reading: https://www.drdobbs.com/the-standard-librarian-containers-of-inc/184403814 – jtbandes May 21 '20 at 17:45
  • And more: https://stackoverflow.com/questions/56975491/why-does-stdvector-work-with-incomplete-types-in-class-definitions, https://www.boost.org/doc/libs/1_73_0/doc/html/container/main_features.html#container.main_features.containers_of_incomplete_types – jtbandes May 21 '20 at 17:46

0 Answers0