0

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'

ildjarn
  • 62,044
  • 9
  • 127
  • 211
SegFault
  • 1,024
  • 2
  • 16
  • 25
  • so..........? the compiler is telling you what is wrong, it doesn't understand this line: `PQueue outgoing_arcs(arcComp);` - what's the question? – Nim Oct 14 '11 at 14:54
  • The question is "why" ? Why doesn't the compiler understand that line? – SegFault Oct 14 '11 at 14:55
  • Why are you including pqueue.cpp? – MGZero Oct 14 '11 at 14:56
  • 1
    Is that line supposed to define a member function or a variable, coz the compiler is seeing it as the first. – UncleBens Oct 14 '11 at 14:56
  • 1
    @SegFault - well the answer depends on what you are trying to do - clearly the compiler has not understood it - are you declaring a data member or member function? If former - see Konrad's answer, if latter, you've not declared it correctly... – Nim Oct 14 '11 at 14:57
  • @MGZero: It's a common way of separating the implementation of template functions from the header file while still having them be visible. – Bill Oct 14 '11 at 14:58
  • @Bill Oh alright, I see. – MGZero Oct 14 '11 at 14:59
  • @Bill, in this case I would rather a different approach - a `.cpp` file could be built by make rules, better to use some other convention such as `foo_cpp.h` - IMO... – Nim Oct 14 '11 at 15:00
  • @Nim: I agree. I've seen the extension .ipp proposed for this (i = implementation). – Bill Oct 14 '11 at 15:03

1 Answers1

9

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    " you cannot (usually) define templates in cpp files", I am seperating interface from implementation by doing so, which I guess is good thing – SegFault Oct 14 '11 at 15:02
  • @SegFault: You might try ".ipp" instead of ".cpp": http://stackoverflow.com/questions/543507/in-the-c-boost-libraries-why-is-there-a-ipp-extension-on-some-header-files – Bill Oct 14 '11 at 15:04
  • 1
    @SegFault It is. But then don’t call the implementation file `pqueue.cpp`. `.cpp` is exclusively reserved to correspond to compilation units! Either use the extension `.ipp` which has become somewhat of a convention, or use something unambiguous as `.impl.hpp` or `.impl.h` or similar. – Konrad Rudolph Oct 14 '11 at 15:05
  • can you please explain what is "nodeT() : ougoing_arcs(arcComp) { }" actually doing? – SegFault Oct 14 '11 at 15:05
  • It's nodeT's constructor, which assigns arcComp to the outgoing_arcs member variable. – Bill Oct 14 '11 at 15:06
  • If I have to use a constructor, then what is the use of using a struct? Is class a better option for nodeT? – SegFault Oct 14 '11 at 15:10
  • 1
    @SegFault There’s almost no difference between class and struct in C++. The only difference that by default everything in a struct is public, whereas in a class it’s private. That’s it. – Konrad Rudolph Oct 14 '11 at 15:16