0

配列へのポインターを持つ構造体を必要とする動的スタックを構築しています。

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
          };

メインファイルで配列を double で埋めようとすると、それを関数に渡しますが、何もしないと正しく渡されないようです。これを行う正しい方法は何ですか?

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;
     }
}     
4

1 に答える 1

0

技術的な問題は、あなたがまだ示していないコード (私がこれを書いているとき)、つまりpushコードにあります。

ただし、どのようにpush物事を台無しにしても機能する簡単な解決策があります。

つまり、std::vector動的に割り当てられた生配列の代わりに a を使用します。

または、各ノードで固定サイズの raw 配列を使用するだけです。

さらに言えばstd::list、DIY リンク リストよりも a の方が優れていますが、おそらく、この演習の全体的なポイントは、リンク リストの構造にある程度慣れることです。各ノードの配列にa を使用してstd::vectorも、その目標は妨げられません。ただし、最新の C++ では、問題が何であれ、自分でリンク リストを作成することが適切な解決策になることはめったにないことに注意してください。代わりに、標準ライブラリのコンテナー クラスやサード パーティ ライブラリのコンテナー クラスを使用してください。

于 2012-12-08T03:13:10.060 に答える