使用ADT在成员函数中无法获得正确的返回值

问题描述:

template<class Type> 
Type* unorderedLinkedList<Type>:: newSearch(nodeType<Type> *head, const Type& x)const 
{ 
    if (head == NULL) 
     return 0; 

    if (head->info == x) 
     return ???(head or *head?); 

    return newSearch(head->link, x); 
} 

template <class Type> 
Type* unorderedLinkedList<Type>::recursiveLinkSeqSearch(const Type& item) const 
{ 
    nodeType<Type> *current; 
    current=this->first; 
    return newSearch(current, item); 


} 

现在我对这个问题感到很蠢,但我无法弄清楚这个函数的返回值。在主代码中,我需要返回一个bool或类似的东西。主要看起来像。使用ADT在成员函数中无法获得正确的返回值

unorderedLinkedList<classType> l1; 
    classType *st; 
    l1.recursiveLinkSeqSearch(st); 
    if(l1){}else{}; 

我已经做了很多关于功能的变化,没有成功,真的很感谢任何帮助。

+0

似乎应该是回报头的问题。你收到了什么错误信息? – Shiping

+0

错误是“无法初始化类型为'dataType *'的返回对象,其类型为'nodeType * –

+0

如何尝试更改”Type * unorderedLinkedList :: newSearch(nodeType * head,const Type&x)const“to “Type * unorderedLinkedList :: newSearch( * head,const Type&x)const”?just remove noteType。 – Shiping

对我来说,看起来你应该返回'头'。如果类型真的要留他们的方式,你必须做一个类型转换:

return (Type*)head; 

但是,也许你想要做的,而不是有什么改变你的函数的返回值:

template<class Type> 
nodeType<Type>* unorderedLinkedList<Type>:: newSearch(nodeType<Type> *head, const Type& x)const 
{ 
// Function body 
} 

template <class Type> 
nodeType<Type>* unorderedLinkedList<Type>::recursiveLinkSeqSearch(const Type& item) const 
{ 
// Function body 
} 

尽管不知道详细信息,但很难说清楚,因此您可能还想做其他事情。

+0

类型转换导致链接器错误,这将工作,但程序是学校,我不能改变递归链接SeqSearch参数。主要是我需要它返回一个布尔值,或布尔类似的价值 –

正如上述评论

return &head->info; 

这个固定的所有类的问题,并解决了数据类型