我對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;
}
}
你的問題缺少一些東西。這將是一個真正的問題。 –