无法使用 setter 编辑存储在链表节点中的数据
Cannot edit data stored in a linked list's node using setter
我的节点class:
class Node {
private:
int orderId; //unique for each node
Cake cake;
Customer customerInfo;
Node* next;
public:
Node() {
orderId = 0;
next = NULL;
}
Node(int id, Cake ck, Customer c) {
orderId = id;
cake = ck;
customerInfo = c;
}
int getOrderId() {
return orderId;
}
Cake getCake() {
return cake;
}
Customer getCustomerInfo() {
return customerInfo;
}
Node* getNext() {
return next;
}
void setOrderId(int id) {
orderId = id;
}
void setCake(Cake ck) {
cake = ck;
}
void setCustomerInfo(Customer c) {
customerInfo = c;
}
void setNext(Node* n) {
next = n;
}
};
我的蛋糕class:
class Cake {
private:
string code;
string flavour;
double weight;
double unitPrice;
int qty;
public:
Cake() {
code = "";
flavour = "";
weight = 0.0;
unitPrice = 0.0;
qty = 0;
}
Cake(string c, string f, double w, double p, int q) {
code = c;
flavour = f;
weight = w;
unitPrice = p;
qty = q;
}
string getCode() {
return code;
}
...other getters
void setCode(string c) {
code = c;
}
...other setters
};
编辑节点的函数:
void editOrder(SinglyLinkedList* s)
{
int orderId, editOption, cakeOption, weightOption, qty, flavourOption;
char next = '[=13=]';
bool isEmpty;
//prompt for order id
cout << "Enter ID of order to be edited: ";
cin >> orderId;
//check if order exists
if (s->checkNodeExist(orderId) != NULL) {
Node* nodeTemp = s->checkNodeExist(orderId);
do {
//ask which aspect to edit
cout << "Which aspect do you want to edit?" << endl;
cout << left << setw(9) << "OPTION" << setw(15) << "ASPECT" << endl;
cout << "----------------------------" << endl << endl;
cout << left << setw(9) << "1" << setw(15) << "Code / Cake Type" << endl;
cout << left << setw(9) << "2" << setw(15) << "Flavour" << endl;
cout << left << setw(9) << "3" << setw(15) << "Weight" << endl;
cout << left << setw(9) << "4" << setw(15) << "Quantity" << endl << endl;
cout << "Enter option: ";
cin >> editOption;
switch (editOption) {
case 1:
//display cake choices & prompt for cake choice
cout << "Choose a type of cake." << endl << endl;
cout << left << setw(9) << "OPTION" << setw(15) << "CAKE" << setw(4) << "CODE" << endl;
cout << "----------------------------" << endl;
cout << left << setw(9) << "1" << setw(15) << "Pound Cake" << setw(4) << "CK01" << endl;
cout << left << setw(9) << "2" << setw(15) << "Sponge Cake" << setw(4) << "CK02" << endl;
cout << left << setw(9) << "3" << setw(15) << "Genoise Cake" << setw(4) << "CK03" << endl;
cout << left << setw(9) << "4" << setw(15) << "Layer Cake" << setw(4) << "CK04" << endl;
cout << left << setw(9) << "5" << setw(15) << "Icing Cake" << setw(4) << "CK05" << endl;
cout << left << setw(9) << "6" << setw(15) << "Cheese Cake" << setw(4) << "CK06" << endl << endl;
cout << "Enter option: ";
cin >> cakeOption;
switch (cakeOption)
{
case 1:
(*nodeTemp).getCake().setCode("CK01"); //FAIL TO UPDATE THE "CODE" USING SETTER
break;
例如,目标节点有一个代码为“CK02”的蛋糕对象,但现在我希望它使用setter将其设置为“CK01”。但我就是想不通为什么 setter 函数不起作用?
PS。刚开始学习 C++ 中的链表和指针,所以请放轻松。非常感谢任何帮助...谢谢:D
这是因为您的 getCake
方法 return 是 copy 您的 Node
对象中的蛋糕。您的 setter 然后更改了副本,但原始文件没有改变。
简单的解决方法是让您的 getter return 成为参考
Cake& getCake() {
return cake;
}
现在getCake
return是对节点中蛋糕的引用,而不是它的副本。
我的节点class:
class Node {
private:
int orderId; //unique for each node
Cake cake;
Customer customerInfo;
Node* next;
public:
Node() {
orderId = 0;
next = NULL;
}
Node(int id, Cake ck, Customer c) {
orderId = id;
cake = ck;
customerInfo = c;
}
int getOrderId() {
return orderId;
}
Cake getCake() {
return cake;
}
Customer getCustomerInfo() {
return customerInfo;
}
Node* getNext() {
return next;
}
void setOrderId(int id) {
orderId = id;
}
void setCake(Cake ck) {
cake = ck;
}
void setCustomerInfo(Customer c) {
customerInfo = c;
}
void setNext(Node* n) {
next = n;
}
};
我的蛋糕class:
class Cake {
private:
string code;
string flavour;
double weight;
double unitPrice;
int qty;
public:
Cake() {
code = "";
flavour = "";
weight = 0.0;
unitPrice = 0.0;
qty = 0;
}
Cake(string c, string f, double w, double p, int q) {
code = c;
flavour = f;
weight = w;
unitPrice = p;
qty = q;
}
string getCode() {
return code;
}
...other getters
void setCode(string c) {
code = c;
}
...other setters
};
编辑节点的函数:
void editOrder(SinglyLinkedList* s)
{
int orderId, editOption, cakeOption, weightOption, qty, flavourOption;
char next = '[=13=]';
bool isEmpty;
//prompt for order id
cout << "Enter ID of order to be edited: ";
cin >> orderId;
//check if order exists
if (s->checkNodeExist(orderId) != NULL) {
Node* nodeTemp = s->checkNodeExist(orderId);
do {
//ask which aspect to edit
cout << "Which aspect do you want to edit?" << endl;
cout << left << setw(9) << "OPTION" << setw(15) << "ASPECT" << endl;
cout << "----------------------------" << endl << endl;
cout << left << setw(9) << "1" << setw(15) << "Code / Cake Type" << endl;
cout << left << setw(9) << "2" << setw(15) << "Flavour" << endl;
cout << left << setw(9) << "3" << setw(15) << "Weight" << endl;
cout << left << setw(9) << "4" << setw(15) << "Quantity" << endl << endl;
cout << "Enter option: ";
cin >> editOption;
switch (editOption) {
case 1:
//display cake choices & prompt for cake choice
cout << "Choose a type of cake." << endl << endl;
cout << left << setw(9) << "OPTION" << setw(15) << "CAKE" << setw(4) << "CODE" << endl;
cout << "----------------------------" << endl;
cout << left << setw(9) << "1" << setw(15) << "Pound Cake" << setw(4) << "CK01" << endl;
cout << left << setw(9) << "2" << setw(15) << "Sponge Cake" << setw(4) << "CK02" << endl;
cout << left << setw(9) << "3" << setw(15) << "Genoise Cake" << setw(4) << "CK03" << endl;
cout << left << setw(9) << "4" << setw(15) << "Layer Cake" << setw(4) << "CK04" << endl;
cout << left << setw(9) << "5" << setw(15) << "Icing Cake" << setw(4) << "CK05" << endl;
cout << left << setw(9) << "6" << setw(15) << "Cheese Cake" << setw(4) << "CK06" << endl << endl;
cout << "Enter option: ";
cin >> cakeOption;
switch (cakeOption)
{
case 1:
(*nodeTemp).getCake().setCode("CK01"); //FAIL TO UPDATE THE "CODE" USING SETTER
break;
例如,目标节点有一个代码为“CK02”的蛋糕对象,但现在我希望它使用setter将其设置为“CK01”。但我就是想不通为什么 setter 函数不起作用?
PS。刚开始学习 C++ 中的链表和指针,所以请放轻松。非常感谢任何帮助...谢谢:D
这是因为您的 getCake
方法 return 是 copy 您的 Node
对象中的蛋糕。您的 setter 然后更改了副本,但原始文件没有改变。
简单的解决方法是让您的 getter return 成为参考
Cake& getCake() {
return cake;
}
现在getCake
return是对节点中蛋糕的引用,而不是它的副本。