模板函数定义的编译错误
compilation errors with template function definition
所以我正在尝试为红黑树制作模板,但我无法弄清楚为什么某些函数无法编译。规格如下:
template <class myType>
class redBlackTree
{
private:
enum treeTraversalOptions {INORDER, PREORDER, POSTORDER, NONE};
enum nodeColor {RED, BLACK};
struct nodeType
{
myType keyValue;
nodeColor color;
unsigned int wrdCount;
nodeType *left;
nodeType *right;
nodeType *parent;
};
nodeType *root;
void destroyTree(nodeType *);
unsigned int countNodes(nodeType *) const;
unsigned int height(nodeType *) const;
nodeType * incCount(myType, nodeType *) const;
unsigned int getWordCount(myType, nodeType *) const;
void getMaxNode(nodeType *, unsigned int &, std::string &);
void printTree(nodeType *, treeTraversalOptions) const;
nodeType * rightRotate(nodeType *);
nodeType * leftRotate(nodeType *);
public:
redBlackTree();
~redBlackTree();
void destroyTree();
unsigned int countNodes() const;
void getMaxNode(unsigned int &, std::string &);
unsigned int height() const;
bool incCount(myType) const;
unsigned int getWordCount(myType) const;
void printTree(treeTraversalOptions) const;
void insert(myType);
};
这是问题函数之一:
template <class MyType>
unsigned int redBlackTree<myType>::getWordCount(myType word, nodeType *treeNode) const
{
if (treeNode)
{
if (treeNode->keyValue == word)
return treeNode->wrdCount;
else if (word < treeNode->keyValue)
getWordCount(word, treeNode->left);
else
getWordCount(word, treeNode->right);
}
return 0;
}
我得到的错误是:'myType' 未在此范围内声明,
模板参数 1 无效,'unsigned int getWordCount'.
的模板声明
我试过在某些地方添加类型名和范围解析运算符,但我就是不明白为什么它不起作用。
您在某些地方使用了MyType
,在其他地方使用了myType
。
您可以通过更改
来修复它
template <class MyType>
至
template <class myType>
或者标准库中的一般约定至少是只使用
template <class T>
它有助于将您的模板化类型与常规变量区分开来。
所以我正在尝试为红黑树制作模板,但我无法弄清楚为什么某些函数无法编译。规格如下:
template <class myType>
class redBlackTree
{
private:
enum treeTraversalOptions {INORDER, PREORDER, POSTORDER, NONE};
enum nodeColor {RED, BLACK};
struct nodeType
{
myType keyValue;
nodeColor color;
unsigned int wrdCount;
nodeType *left;
nodeType *right;
nodeType *parent;
};
nodeType *root;
void destroyTree(nodeType *);
unsigned int countNodes(nodeType *) const;
unsigned int height(nodeType *) const;
nodeType * incCount(myType, nodeType *) const;
unsigned int getWordCount(myType, nodeType *) const;
void getMaxNode(nodeType *, unsigned int &, std::string &);
void printTree(nodeType *, treeTraversalOptions) const;
nodeType * rightRotate(nodeType *);
nodeType * leftRotate(nodeType *);
public:
redBlackTree();
~redBlackTree();
void destroyTree();
unsigned int countNodes() const;
void getMaxNode(unsigned int &, std::string &);
unsigned int height() const;
bool incCount(myType) const;
unsigned int getWordCount(myType) const;
void printTree(treeTraversalOptions) const;
void insert(myType);
};
这是问题函数之一:
template <class MyType>
unsigned int redBlackTree<myType>::getWordCount(myType word, nodeType *treeNode) const
{
if (treeNode)
{
if (treeNode->keyValue == word)
return treeNode->wrdCount;
else if (word < treeNode->keyValue)
getWordCount(word, treeNode->left);
else
getWordCount(word, treeNode->right);
}
return 0;
}
我得到的错误是:'myType' 未在此范围内声明, 模板参数 1 无效,'unsigned int getWordCount'.
的模板声明我试过在某些地方添加类型名和范围解析运算符,但我就是不明白为什么它不起作用。
您在某些地方使用了MyType
,在其他地方使用了myType
。
您可以通过更改
来修复它template <class MyType>
至
template <class myType>
或者标准库中的一般约定至少是只使用
template <class T>
它有助于将您的模板化类型与常规变量区分开来。