这个问题在这里已有答案:
我在C编程 . 构造函数中的数组值与get_data的成员函数不同 .
for(int k = 0; k < 8; k++)
{
cout << "The invoice for " << *(categories_invo + k) << endl;
}
和,
for(int k = 0; k < 8; k++)
{
cout << "The invoice for get_data function is " << *(categories_invo + k) << endl;
}
get_data函数中的数组值似乎是随机的 . 有人能找出错误吗?谢谢 .
#include <iostream>
#include <cstdlib>
using namespace std;
class Invoice
{
friend class FuncInvoice;
private:
int uni_num;
string month;
float amount;
string *name_categories;
float *categories_invo;
Invoice *next;
public:
Invoice(int uni_num, float amount, string month, string *n_c, float *cate)
{
this->uni_num = uni_num;
this->amount = amount;
this->month = month;
name_categories = n_c;
categories_invo = cate;
next = 0;
for(int k = 0; k < 8; k++)
{
cout << "The invoice for " << *(categories_invo + k) << endl;
}
}
Invoice *get_next()
{
return next;
}
void get_data()
{
cout << "The invoices for " << month << endl;
cout << "The unique invoice number is: " << uni_num << endl;
for(int k = 0; k < 8; k++)
{
cout << "The invoice for get_data function is " << *(categories_invo + k) << endl;
}
}
};
class FuncInvoice
{
private:
Invoice *head;
public:
FuncInvoice()
{
head = 0;
}
Invoice *begin()
{
return head;
}
void insert(Invoice *n)
{
n->next = head;
head = n;
}
};
FuncInvoice lists;
void add();
void display();
int main()
{
add();
display();
system("pause");
return 0;
}
void add()
{
int i = 0;
int uni_num;
string mon_name[12] = {"Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};
string name_categories[8] = {"fuel", "food", "clothing", "holidays", "gifts", "gym", "books", "movies"};
float categories[8];
string command;
do
{
cout << "The invoices are recorded for: " << mon_name[i] << endl;
cout << "The unique number is: " << endl;
cin >> uni_num;
float amount = 0;
for(int j = 0; j < 8; j++)
{
cout << "The invoice for " << name_categories[j] << " is:" << endl;
cin >> categories[j];
amount = amount + categories[j];
}
Invoice *object = new Invoice(uni_num, amount, mon_name[i], name_categories, categories);
lists.insert(object);
i++;
cout << "Do you want to finish input? Please type \"yes\" or \"no\":" << endl;
cin >> command;
} while(i < 12 && (command != "yes"));
}
void display()
{
Invoice *object;
for(object = lists.begin(); object != 0; object = object->get_next())
{
object->get_data();
}
}
结果显示:
The invoices are recorded for: Jan
The unique number is:
1
The invoice for fuel is:
1
The invoice for food is:
2
The invoice for clothing is:
3
The invoice for holidays is:
4
The invoice for gifts is:
5
The invoice for gym is:
6
The invoice for books is:
7
The invoice for movies is:
8
The invoice for 1
The invoice for 2
The invoice for 3
The invoice for 4
The invoice for 5
The invoice for 6
The invoice for 7
The invoice for 8
Do you want to finish input? Please type "yes" or "no":
yes
The invoices for Jan
The unique invoice number is: 1
The invoice for get_data function is 1.4013e-045
The invoice for get_data function is 1.4013e-045
The invoice for get_data function is 1.54143e-044
The invoice for get_data function is 3.69695e+033
The invoice for get_data function is 1.01927e-038
The invoice for get_data function is 1.01926e-038
The invoice for get_data function is 1.01927e-038
The invoice for get_data function is 6.4661e-039