2016-08-19 37 views
-2

嘿家伙,所以我得到这个错误,当我试图为我的链表类创建此函数。我遇到的问题是我的搜索功能。我还没有开始创建函数,但是我收到的错误是在搜索函数的声明中。在NODEPTR线下38它说,这是不确定的,并在搜索它说错误:与声明“的LinkedList :: NODEPTR不兼容(在线17声明)的代码如下任何帮助表示赞赏链接列表...声明与原型不兼容

// LinkedListProject.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <iostream> 
#include <list> 
using namespace std; 
class LinkedList { 
public: 
    struct Node { 
     int data; 
     Node* link; 
    }; 
    typedef Node* NodePtr; 
    //NodePtr head = new Node; 
    void head_insert(NodePtr& head, int the_number); 
    NodePtr search(NodePtr head, int target); 
private: 
}; 


int main() 
{ 
    LinkedList obj; 
    //obj.head->data = 3; 
    //obj.head->link = NULL; 
    return 0; 
} 


void LinkedList::head_insert(NodePtr& head, int the_number) { 
    NodePtr temp_ptr = new Node; 
    temp_ptr->data = the_number; 
    temp_ptr->link = head; 
    head = temp_ptr; 
} 

NodePtr LinkedList::search(NodePtr head, int target) 
{ 
    return NodePtr(); 
} 
+0

在实现中更改返回类型“LinkedList :: NodePtr”而不是“NodePtr”。 –

+0

或者,'auto LinkedList :: search(NodePtr head,int target) - > NodePtr' – aschepler

回答

1

NodePtr是。一个名字作用域为你的类。为了使用它,你需要LinkedList::NodePtr类之外。所以,你必须改变

NodePtr LinkedList::search(NodePtr head, int target) 

LinkedList::NodePtr LinkedList::search(NodePtr head, int target) 

现在你可能会问,“别急,我并不需要它在搜索,怎么办?”,而答案是你做

LinkedList::search 

后的类名注入的休息功能的范围。因此,我们不需要明确限定任何适用于该类的名称。

2

您必须在定义NodePtr的位置设置正确的范围。

LinkedList::NodePtr LinkedList::search(NodePtr head, int target) 
{ 
    return LinkedList::NodePtr(); 
} 
+0

抱歉,正确的单词是作用域,编辑过 – pospich0815