我正在构建一个动态堆栈,该堆栈需要一个带有指向数组的指针的结构。
class studentstack
{
private:
struct StackNode
{
int ID;
string Name;
string Address;
StackNode * next; // pointer to the next node
double * scores; // pointer to the arry of scores
};
当我在我的主文件中尝试用双精度填充数组然后将它传递给一个函数时,当我什么都不做时似乎正确传递。这样做的正确方法是什么?
int main()
{
studentstack s;
string name;
int id;
string address;
double score;
for(int x =0; x<20; x++)
{
cout << "\nNew Student Name: ";
cin >> name;
cout << "\nID: ";
cin >> id;
cout << "\nAddress: ";
cin >> address;
double scoresArr[10];
for(int z=0; z<10; z++)
{
cout << "\nStudent Score " << z+1 << ": ";
cin >> score;
scoresArr[z] = score;
}
s.push(name, id, address, scoresArr);
推:
void studentstack::push(string name, int id, string address, double scoresArr)
{
StackNode *newStudent; // To point to the new Student
newStudent = new StackNode;
newStudent-> ID = id;
newStudent-> Name = name;
newStudent-> Address = address;
newStudent-> scores = scoresArr;
// If there are no nodes in the stack
if (isEmpty())
{
top = newStudent;
newStudent->next= NULL;
}
else // or add before top
{
newStudent->next = top;
top = newStudent;
}
}