Pergunta

In file main.cpp...

#include "pqueue.h"

struct nodeT;

struct coordT {
    double x, y;
};

struct arcT {
    nodeT *start, *end;
    double weight;
};

int arcComp(arcT *arg0, arcT *arg1){
    if(arg0->weight == arg1->weight)
        return 0;
    else if(arg0->weight > arg1->weight)
        return 1;
    return -1;
}

struct nodeT {
    coordT* coordinates;
    PQueue<arcT *> outgoing_arcs(arcComp); // error on this line
};

In file pqueue.h ...

#ifndef _pqueue_h
#define _pqueue_h

template <typename ElemType>
class PQueue 
{
private:
    typedef int (*CallbackFunc)(ElemType, ElemType);
    CallbackFunc CmpFunc;

public:
    PQueue(CallbackFunc Cmp);
    ~PQueue();  
};

#include "pqueue.cpp"
#endif

In file pqueue.cpp

#include "pqueue.h"

template <typename ElemType>
PQueue<ElemType>::PQueue(CallbackFunc Cmp = OperatorCmp)
{
    CmpFunc = Cmp;
}

template<typename ElemType>
PQueue<ElemType>::~PQueue()
{
}

error C2061: syntax error : identifier 'arcComp'

Foi útil?

Solução

The syntax is simply invalid, you cannot initialise members in-place; use a constructor.

struct nodeT {
    coordT* coordinates;
    PQueue<arcT *> outgoing_arcs;

    nodeT() : ougoing_arcs(arcComp) { }
};

Apart from that you cannot (usually) define templates in cpp files, you must put the complete definition inside the header file. Granted, you are #includeing the cpp file rather than treating it as a separate compilation unit but that’s still bad, if only because it will trip up programmers’ expectations and automated build tools.

As a final side-note, your code is violating every single C++ naming convention I have ever encountered.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top