0

ファイル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
};

ファイル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

ファイルpqueue.cpp内

#include "pqueue.h"

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

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

エラーC2061:構文エラー:識別子'arcComp'

4

1 に答える 1

9

構文は単に無効であり、メンバーをインプレースで初期化することはできません。コンストラクターを使用します。

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

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

それとは別に、(通常)cppファイルでテンプレートを定義することはできませんが、完全な定義をヘッダーファイル内に配置する必要があります。確かに#include、cppファイルを個別のコンパイル単位として扱うのではなく、使用していますが、プログラマーの期待と自動ビルドツールが機能しなくなるという理由だけで、それでも問題があります。

最後の補足として、あなたのコードは私が今まで出会ったすべてのC++命名規則に違反しています。

于 2011-10-14T14:55:58.443 に答える