2016-11-13 48 views
-1

我对C++很陌生,目前试图在用户可以设置的链表中插入一个电话号码和名称。我想知道我错了哪里,我一直在观看视频后的视频,但一切都是关于在链表中使用整数而不是字符串。C++与字符串的链接列表问题

因此,如果主被取出程序显示(0)错误和(0)警告,所以我认为这个问题是主要的。

所以在主要我想辛(这是我有内置的函数部分set_first)。我试过把它传给一个地址,但我认为我只是用这种方式来解决问题。

所以问题是:任何人都可以指向正确的方向,或者让我知道如何将名称和电话号码放入用户指定的节点中?

这是我的计划,从主迄今降:

#include <iostream> 
#include <cstdlib> 
#include "l_List_1.h" 

using namespace std; 

int main() 
{ 
    l_List_1 obr; 

    std::string nam; 
    cout << "name"; 
    cin >> nam; 
    obr.set_first(nam); //I'm trying to give it the nam since that's what I have inside my set_first function. 



return 0; 
} 

我的头文件是这样的:

#ifndef L_LIST_1_H 
#define L_LIST_1_H 


class l_List_1 
{ 
public: 
    l_List_1(); 
    void set_first(std::string nam, int phnum); 
    void delete_Node(std::string del_nams, int num_del); 
    void display(); 
    char menu(); 


private: 
    typedef struct node { 
     std::string user_Name; 
     int user_Phone; 
     node* next; 
    }* nodeptr; 

    nodeptr head; 
    nodeptr curr; 
    nodeptr tail; 
    nodeptr temp; 
}; 

#endif // L_LIST_1_H 

和我的cpp文件是在这里:

#include <iostream> 
#include <cstdlib> 
#include <string> 
#include "l_List_1.h" 

using namespace std; 


l_List_1::l_List_1() 
{ 
    head = NULL; 
    curr = NULL; 
    tail = NULL; 
} 

void l_List_1::set_first(std::string nam, int phnum) { 
    nodeptr n = new node; 
    n->next = NULL; 
    n->user_Name = nam; 
    n->user_Phone = phnum; 

    cout << "name"; 
    cin >> nam; 

    if (head != NULL) { 
     curr = head; 
     while (curr->next != NULL) { 
      curr = curr->next; 
     } 
     curr->next = n; 
} 
    else { 
     head = n; 
    } 
} 

void l_List_1::delete_Node(std::string del_nams, int num_del){ 
    nodeptr delptr = NULL; 
    temp = head; 
    curr = head; 
    while(curr != NULL && curr->user_Name != del_nams && curr->user_Phone != num_del){ 
     temp = curr; 
     curr = curr->next; 
    } 
    if(curr == NULL){ 
     cout << del_nams << "Was not in the list"; 
     cout << num_del << "was not in the list"; 
     delete delptr; 
    } else{ 
     delptr = curr; 
     curr = curr-> next; 
     temp->next = curr; 
     delete delptr; 
     cout << "the item" << del_nams << "was deleted"; 
     cout << "the item" << num_del << "was deleted"; 
    } 
} 

void l_List_1::display(){ 
    curr = head; 
    while(curr != NULL){ 
     cout << curr->user_Name; 
     cout << curr->user_Phone << endl; 
     curr = curr->next; 
    } 
} 
+1

你的问题缺少一些东西。这将是一个真正的问题。 –

回答

0

对于启动器成员函数set_first用两个参数声明

void set_first(std::string nam, int phnum); 

但是你有一个说法

obr.set_first(nam); 

在函数本身(在删除节点的第二功能)您没有设置列表的尾部调用它。

而且它目前尚不清楚这些语句在功能上做

cout << "name"; 
cin >> nam; 

你应该删除它们。

而且是没有意义的声明以下数据成员

nodeptr curr; 
nodeptr temp; 

来代替他们,你可以在方法中使用局部变量。

考虑到,你应该包括头<string>

#include <string> 

,我没有看到从标题<cstdlib>的东西在你的程序中使用。您可以删除标题。

+0

感谢您的帮助。 – Gus

+0

@Gus没有。:) –