gpt4 book ai didi

C++ 结构和类编译器错误

转载 作者:行者123 更新时间:2023-11-27 23:06:27 26 4
gpt4 key购买 nike

我遇到的两大错误是:

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

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

我的 .cpp 文件是:

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

我的 .h 文件是:

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


};

任何帮助将不胜感激,我已经开始使用它 3 个小时了,但不知道如何修复它。

最佳答案

您的struct node 似乎有一个引用成员,例如:

struct node
{
int& num;
};

该成员应该在构造函数的初始化列表中初始化。 (引用不能为空...)

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

int& num;
};

此外,您正在访问一个指针,就像它是一个值一样:

sInfoHEAD.a_student -> firstName

应该是:

sInfoHEAD->a_student -> firstName
// ^^

最后,当您将对象作为参数传递时,尽可能将它们作为引用或 const 引用传递:

int insertEntry(const studentInfo  entry_name);

应该变成:

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

而且,正如我在评论中所说:using namespace std; 在头文件中不是一个好习惯。如果需要,您可以阅读:https://stackoverflow.com/a/5849668/1394283

关于C++ 结构和类编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23238059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com