【发布时间】:2011-10-14 14:51:34
【问题描述】:
在文件 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'
【问题讨论】:
-
所以..........?编译器告诉你出了什么问题,它不明白这一行:
PQueue<arcT *> outgoing_arcs(arcComp);- 有什么问题? -
问题是“为什么”?为什么编译器不理解那行?
-
为什么要包含 pqueue.cpp?
-
该行应该定义成员函数还是变量,因为编译器将其视为第一个。
-
@SegFault - 答案取决于你想要做什么 - 显然编译器没有理解它 - 你是在声明数据成员还是成员函数?如果是前者 - 请参阅 Konrad 的回答,如果是后者,则说明您没有正确声明...
标签: c++ class-template