如何定义静态泛型成员

时间:2019-05-28 16:18:40

标签: c++ c++11 generic-programming

尝试编译程序时出现此错误:

minpriorityqueue.cpp:4:36: error: expected expression std::map <MinPriorityQueue<G>::node*,int> MinPriorityQueue<G>::positions = {{}};

,并且在逗号下带有'^'符号

// graph.h
#include "minpriorityqueue.h"
#include "node.h"
#include "edge.h"
class Traits {
    public:
        typedef char N;
    typedef int E;
};

template <typename Tr>
class Graph {
    public:
        typedef Graph<Tr> self;
        typedef MinPriorityQueue<self> min_priority_queue;
        typedef Node<self> node;

        struct u {
            node* n;
            E key;
            struct u* parent;
            u(node* n) : n(n), key(INT_MAX), parent(nullptr) {}
        };
        typedef struct u U;

    self MST_Prim(node* r) {
        // some code
        min_priority_queue::heapDecreaseKey(V, v, weight(u->n,v->n));
        // more code
        self MST;
        // and more code...
        return MST;
    }
};
// minpriorityqueue.h
template <typename G>
class MinPriorityQueue{
    private:
        typedef typename G::U U;
        typedef typename G::E E;
        typedef typename G::node node;
        static std::map<node*,int> positions;
    public:
        static void heapDecreaseKey(std::vector<std::pair<node*, U*> >& A, U* n, E key) {
            // implementation
        }
};
// minpriorityqueue.cpp
#include "minpriorityqueue.h"

template<typename G>
std::map<MinPriorityQueue<G>::node*,int> MinPriorityQueue<G>::positions = {{}};

我已经尝试在MinPriorityQueue<G>::之前定义没有node的职位,这给了我另一个错误。我还尝试使用typename MinPriorityQueue<G>::并获得以下信息:

Undefined symbols for architecture x86_64:
"MinPriorityQueue<Graph<Traits> >::positions", referenced from:

我将近一天一直在尝试解决此问题,但找不到与此类似的任何东西。

0 个答案:

没有答案