开发者

C2143 in nested template class

开发者 https://www.devze.com 2023-03-12 13:56 出处:网络
template <class T> class Edge开发者_如何学Python; template <class T> class Vertex; template <class T>
template <class T>
class Edge开发者_如何学Python;

template <class T>
class Vertex;

template <class T>
class Vertex
{
    T key;
    char color;
    std::vector<Edge> adjVertices;
};

template <class T>
class Edge
{
    Vertex* source;
    Vertex* target;
};

Gives me error in line: std::vector< Edge > adjVertices; error: error C2143: syntax error : missing ';' before '<' see reference to class template instantiation 'ds::Vertex<T>' being compiled.

What should I change?


Your must

#include <vector>

and change

std::vector<Edge> adjVertices;

to

std::vector<Edge<T>> adjVertices;

The same applies to:

Vertex<T>* source;
Vertex<T>* target;

Next you'll face the problem that Vertex is defined before Edge. You should reverse the order.


This compiles:

#include <vector>

template <class T>
class Edge;

template <class T>
class Vertex;

template <class T>
class Vertex
{
    T key;
    char color;
    std::vector<Edge <T> > adjVertices;
};

template <class T>
class Edge
{
    Vertex <int> * source;
    Vertex <int> * target;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消