lundi 28 janvier 2019

c++ template Incomplete type using pointer of class inside template

I am trying to create a grid, From what I understand the issue is coming from using a pointer of a template class inside its self which is legal until i try to do stuff with it is when the compiler complains. I am looking for a way to use pointer's to a class of a template inside its self for use in using the pointers and do what I will later. I compile with g++ version 5 the compile command i use is g++ *.cpp -o main -std=c++11 the error i get will follow the snippet for the code.

struct Vector2D 
{
    Vector2D(  ) {  }
    Vector2D( int x , int y ): x( x ) , y( y ) {  } ;

    int x , y ; 

} ;

template <typename A>
class GridNode2D ; 

template <typename T>
class GridNode2D
{
public: 
    GridNode2D(  ) {  } ;
    T data ; 
    Vector2D coOrdinate ; 

    GridNode2D<T>* left, right, up, down ; 

} ;

template <typename T>
class Grid2D
{
public:
    Grid2D(  ) ;

    GridNode2D<T>* head ; 

} ;

template <typename T>
Grid2D<T>::Grid2D(  )
{
    this->head = new GridNode2D<T> ; 
    this->head->right = new GridNode2D<T> ; 

} ;

Errors:

main.cpp: In instantiation of ‘class GridNode2D<bool>’:
<span class="error_line" onclick="ide.gotoLine('main.cpp',39)">main.cpp:39:16</span>:   required from ‘Grid2D<T>::Grid2D() [with T = bool]’
<span class="error_line" onclick="ide.gotoLine('main.cpp',47)">main.cpp:47:18</span>:   required from here
main.cpp:22:26: error: ‘GridNode2D::right’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                          ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D
       ^
main.cpp:22:33: error: ‘GridNode2D::up’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                                 ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D
       ^
main.cpp:22:37: error: ‘GridNode2D::down’ has incomplete type
     GridNode2D<T>* left, right, up, down ; 
                                     ^
main.cpp:15:7: note: definition of ‘class GridNode2D’ is not complete until the closing brace
 class GridNode2D

Aucun commentaire:

Enregistrer un commentaire