【问题标题】:Getting Error When Trying to Call Function from Another Header File (C++)尝试从另一个头文件调用函数时出错 (C++)
【发布时间】:2014-12-04 03:34:19
【问题描述】:

我开发了一个双参数二叉搜索树,现在正尝试将它应用到创建字典。我在树中的 find 函数正常工作,但是,当我尝试在我的字典类中调用它时,我得到一个编译器错误“预期在 '>' 标记之前的主表达式”。谁能帮我理解出了什么问题?我对尝试从其他文件调用函数不是很熟悉。

这是我从 bst.h 内部的二叉搜索树中找到的函数

    V & find (K key)                      
    {

    TreeNode<K,V> *traverseFind;

    traverseFind = root; 
    unsigned int compareTraverseToHeight = 0;

    while (compareTraverseToHeight < heightCount) 
    {
        if (traverseFind == NULL)  // Fallen off
        {
            keyFinder = 0;
            break;

        }

        else if ( key == traverseFind->key )// Found key
        {
            keyFinder = 1;
            break;
        }       

        else if ( key < traverseFind->key )                   
        {                                                 .
            traverseFind = traverseFind->left;
        }   


        else
        {
            traverseFind = traverseFind->right;
        }  


         compareTraverseToHeight++;      

    }  // end of loop


            if(keyFinder ==0)
            {
                throw key_not_found_exception();

            }


        cout<<"RETURNED "<<traverseFind->value<<endl;

        return traverseFind->value;

}

还有字典:

    #include bst.h

    template <class K, class V> class Dictionary
    {
    public:

        BinarySearchTree<K,V> wiktionary;

        Dictionary()
        {
        }

        ~Dictionary()
        {
        }

    V & find (K key)
    {

        return(wiktionary.find(<V>));  //this is the issue

    }



    private:


    };

    #endif

以及 main 中的应用程序:

int main()
{

    Dictionary<string, int> d;
    int val = d.find("abc");
    if (val != 15)
        throw dictionary_tester_exception(__FILE__, __LINE__);

    return 0;

}

【问题讨论】:

  • 使用wiktionary.find(key) :/
  • 哦,哇,不知道我怎么没听懂。将此作为答案,我会接受,谢谢。

标签: c++ binary-search-tree header-files


【解决方案1】:

问题是&lt;V&gt; 指定了一个模板参数,但您试图将它作为函数参数传递给BinarySearchTree::find()find 成员函数没有模板化,因此即使您尝试将其作为模板参数传递也不起作用。根据提供的代码,您应该将key 作为参数传递给find,就像这样

return(wiktionary.find(key));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2019-10-14
    • 1970-01-01
    相关资源
    最近更新 更多