【发布时间】:2019-06-21 10:31:54
【问题描述】:
我正在尝试创建一个网格,据我了解,问题来自在其自身内部使用模板类的指针,这是合法的,直到我尝试用它做事时编译器抱怨。我正在寻找一种在其自身内部使用指向模板类的指针的方法,以用于使用指针并执行我稍后将要做的事情。我用 g++ 版本 5 编译我使用的编译命令是 g++ *.cpp -o main -std=c++11 我得到的错误将遵循代码的 sn-p。
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> ;
} ;
错误:
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
【问题讨论】:
标签: c++ c++11 templates pointers compilation