Seg*_*ult 0 c++ class-template
在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
};
Run Code Online (Sandbox Code Playgroud)
在文件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
Run Code Online (Sandbox Code Playgroud)
在文件pqueue.cpp中
#include "pqueue.h"
template <typename ElemType>
PQueue<ElemType>::PQueue(CallbackFunc Cmp = OperatorCmp)
{
CmpFunc = Cmp;
}
template<typename ElemType>
PQueue<ElemType>::~PQueue()
{
}
Run Code Online (Sandbox Code Playgroud)
错误C2061:语法错误:标识符
'arcComp'
语法无效,您无法就地初始化成员; 使用构造函数.
struct nodeT {
coordT* coordinates;
PQueue<arcT *> outgoing_arcs;
nodeT() : ougoing_arcs(arcComp) { }
};
Run Code Online (Sandbox Code Playgroud)
除此之外,您不能(通常)在cpp文件中定义模板,您必须将完整的定义放在头文件中.当然,您正在#include使用cpp文件,而不是将其视为单独的编译单元,但这仍然很糟糕,只是因为它会使程序员的期望和自动构建工具绊倒.
作为最后的注释,您的代码违反了我遇到的每个C++命名约定.