简体   繁体   English

无法识别类模板的C ++成员函数

[英]C++ member function of class template not recognized

I'm working on a binary search tree class and implementing a Find operation. 我正在研究二叉搜索树类并实现Find操作。 There are two versions of the public function, one which returns a const Node* and is const and the other which returns a non-const Node* and is not itself const. 公共函数有两种版本,一种返回一个const Node *并为const,另一种返回一个非const Node *而不本身为const。 Here are the public definitions: 以下是公共定义:

const Node *Find(const T &t) const { Find(t, m_root); }

Node *Find(const T &t) { Find(t, m_root); }

And here's the definition of the private Find method: 这是私有Find方法的定义:

template <typename T>
const Node* CTree<T>::Find(const T &t, Node *root) const {
    if (root == 0)
        return Node();
    else if (t < root->m_number)
        Find(t, root->m_ll);
    else if (t > root->m_number)
        Find(t, root->m_rl);
    else
        return this;
}

Visual Studio is telling me that "'Find' : member function not declared in 'CTree'". Visual Studio告诉我“'查找':未在'CTree'中声明的成员函数”。 Why would it say that? 为什么这么说呢?

Edit to add specifics of error messages: There are 5 messages for the line of the private method definition starting const Node*... 编辑以添加错误消息的详细信息:私有方法定义行的const Node*...行有5条消息const Node*...

Missing type specifier - int assumed

Syntax error : missing ';' before '*'

'T' : undeclared identifier

'CTree' : 'T' is not a valid template type argument for parameter 'T'

syntax error : missing ',' before '&'

Then one more error for the closing brace of the definition (this is the one that I believe is causing the other 5): 然后是定义的右括号的另一个错误(我认为这是导致其他5个错误的原因):

'Find' : member function not declared in 'CTree'

As a note, Visual Studio is not highlighting Node in the definition of Find as it does elsewhere. 需要注意的是,Visual Studio没有像其他地方那样在Find的定义中突出显示Node

Here's the whole class: http://pastebin.com/JEEZJD4n 这是整个课程: http : //pastebin.com/JEEZJD4n

Mistake is in method definition (line 93 of pastebin): 方法定义中存在错误(pastebin的第93行):

template <typename T>
const Node* CTree<T>::Find(const T &t, Node *root) const {

You don't have any type Node in global scope, because your class Node in nested. 您在全局范围内没有任何类型的Node ,因为您的class Node是嵌套的。 So first fix will be an addition of the parent class qualification: 因此,第一个解决方法是增加父类的资格:

template <typename T>
const CTree<T>::Node* CTree<T>::Find(const T &t, Node *root) const {

But, there is another problem: now, compiler cannot parse it right way. 但是,还有另一个问题:现在,编译器无法正确解析它。 You must add typename keyword, to indicate a nested type: 您必须添加typename关键字,以指示嵌套类型:

template <typename T>
const typename CTree<T>::Node* CTree<T>::Find(const T &t, Node *root) const {

See " When is the “typename” keyword necessary? " for details. 有关详细信息,请参见“ 何时需要“ typename”关键字? ”。

Notes: 笔记:

  • You can avoid any of those problems if you put method definition directly to a class body (depending on your code style). 如果将方法定义直接放在类主体中(取决于您的代码样式),则可以避免任何这些问题。
  • You could broadly enrich your coding experience, and make your template coding more productive, if you could use different compilers. 如果可以使用其他编译器,则可以广泛地丰富您的编码经验,并使模板编码更高效。 Note how g++ is clearer in this kind of error: 请注意,在这种错误中g++如何更清晰:

     main.cpp : At global scope : main.cpp : 104 : 7 : error : 'Node' does not name a type const Node* CTree<T>::Find(const T &t, Node *root) const ^ main.cpp: At global scope: main.cpp:104:7: error: need 'typename' before 'CTree<T>::Node' because 'CTree<T>' is a dependent scope const CTree<T>::Node* CTree<T>::Find(const T &t, Node *root) const ^ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM