简体   繁体   中英

C++ Struct and Class Compiler Errors

The big two errors I'm getting are:

uninitialized reference member in 'struct node' using 'new' without new-initializer.

and

request for member 'a_student' in '((score*)this->score::sInfoHEAD', which is of non-class type 'node'.

My .cpp file is:

    #include "CS163Program1.h"
    using namespace std;

    score::score()
    {
      sInfoHEAD = '\0';
      profHEAD = '\0';
    }

   score::~score()
    {
      delete sInfoHEAD;
      delete profHEAD;
    }

   int score::insertEntry(studentInfo a_student)
   {
        if(!sInfoHEAD)
        {
             sInfoHEAD = new node;
             sInfoHEAD -> a_student = new studentInfo;

              sInfoHEAD.a_student -> firstName = new char[strlen(a_student.firstName)+1];
              strcpy(firstName, a_student.firstName);

              sInfoHEAD.a_student -> lastName = new char[strlen(a_student.lastName)+1];
              strcpy(lastName, a_student.lastName);

              sInfoHEAD.a_student -> program1_grade = a_student.program1_grade;
              sInfoHEAD.a_student -> program2_grade = a_student.program2_grade;
              sInfoHEAD.a_student -> program3_grade = a_student.program3_grade;
              sInfoHEAD.a_student -> program4_grade = a_student.program4_grade;
              sInfoHEAD.a_student -> program5_grade = a_student.program5_grade;
              sInfoHEAD.a_student -> midterm_grade = a_student.midterm_grade;
              sInfoHEAD.a_student -> final_grade = a_student.final_grade;
              sInfoHEAD.a_student -> labAttendance = a_student.labAttendance;
              sInfoHEAD -> next = NULL;
      }

}

My .h file is:

    #include <iostream>
    #include <cstring>
    #include <cctype>
    using namespace std;

    struct studentInfo
    {
      char * firstName;
      char * lastName;
      int program1_grade;
      int program2_grade;
      int program3_grade;
      int program4_grade;
      int program5_grade;
      int midterm_grade;
      int final_grade;
      int labAttendance;
      int total_grade;

    };

    class score
    {
      public:
              score();
              ~score();
              int insertEntry(const studentInfo  entry_name);
             int updateEntry(const studentInfo  entry_name);
              int displayAll();
              int displaySpecific(profDemo proficiency);
              int evaluate(profDemo proficiency);
              int buildTestStruct(const studentInfo & a_student);

      private:
              node * sInfoHEAD;
              node * profHEAD;


     };

any help would be vastly appreciated, I've been starting at it a good 3 hours and do not know how to fix it.

Your struct node seems to have a reference member like:

struct node
{
    int& num;
};

This member should be initialize in the initializer-list in the constructor. (a reference cannot be null...)

struct node
{
    node( int& iNum ) : num( iNum ) {}

    int& num;
};

Also, you are accessing a pointer like if it was a value:

sInfoHEAD.a_student -> firstName

Should be :

sInfoHEAD->a_student -> firstName
//       ^^

And finally, when you are passing objects as arguments, try to pass them as references or const references when it is possible :

int insertEntry(const studentInfo  entry_name);

Should become :

int insertEntry(const studentInfo& entry_name);
//                               ^

And also, like I said in the comment: using namespace std; in header file is not a good practice. You can read this if you want: https://stackoverflow.com/a/5849668/1394283

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM