我正在为学校编写一个项目,其中不允许使用任何外部库(这真的很糟糕,因为我必须根据项目要求制作动态数组而不是向量)。我相信问题是当我为动态数组运行构造函数时,它没有为我的数组分配容量。贝娄是我的构造函数。
Roster::Roster(int capacity) {
capacity = capacity;
size = 0;
Student* classRosterArray = new Student[capacity];
然后我在主函数中使用
Roster classRosterArray(5);
来调用它。当我查看调试时,它显示 classRosterArray 的容量等于 0。我查看了其他人为作业所做的事情,他们做了
Roster::Roster(int capacity) {
this->capacity = capacity;
this->size = 0;
this->classRosterArray = new Student * [capacity];
但是即使当我尝试它在调试中仍然说容量为 0 并且仍然抛出异常,输出中唯一的区别是它指定的 this 是 0xFDFDFE75 (尝试在两侧做两个星号,但是只是粗体)我认为这只是程序试图放入没有任何容量的数组中的指针。我对 C++ 还很陌生,这些指针一直让我感到非常痛苦。我将把所有代码放在下面,以防它不是我想的那样,但从我所有的调试和返工来看,这似乎是断点。
主.cpp
#include <iostream>
#include "degree.h"
#include "student.h"
#include "roster.h"
using namespace std;
int commaSpotStart = 0;
int commaSpotEnd;
int commaDist;
string scrubbedID;
string scrubbedFName;
string scrubbedLName;
string scrubbedEmail;
string scrubbedAge;
string scrubbedD1;
string scrubbedD2;
string scrubbedD3;
string scrubbedDegree;
int convAge;
int convD1;
int convD2;
int convD3;
DegreeProgram convDegree;
void scrub(string stringStudent) {
commaSpotStart = 0;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
scrubbedID = stringStudent.substr(commaSpotStart, commaSpotEnd);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedFName = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedLName = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart); commaDist = commaSpotEnd - commaSpotStart;
scrubbedEmail = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedAge = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedD1 = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedD2 = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedD3 = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
commaSpotEnd = stringStudent.find(",", commaSpotStart);
commaDist = commaSpotEnd - commaSpotStart;
scrubbedDegree = stringStudent.substr(commaSpotStart, commaDist);
commaSpotStart = commaSpotEnd + 1;
}
void printStringStudent() {
int i = 0;
std::cout << scrubbedID << "\tFirst Name: " << scrubbedFName << "\tLast Name: " << scrubbedLName << "\tAge: " << scrubbedAge << "\tdaysInCourse: {";
std::cout << scrubbedD1 << ", " << scrubbedD2 << ", " << scrubbedD3 << "} Degree Program: " << scrubbedDegree << std::endl;
}
void convertAge() {
convAge = 0;
for (int i = 0; i < scrubbedAge.length(); i++) {
convAge = convAge * 10 + scrubbedAge[i] - '0';
}
}
void convertD1() {
convD1 = 0;
for (int i = 0; i < scrubbedD1.length(); i++) {
convD1 = convD1 * 10 + scrubbedD1[i] - '0';
}
}
void convertD2() {
convD2 = 0;
for (int i = 0; i < scrubbedD2.length(); i++) {
convD2 = convD2 * 10 + scrubbedD2[i] - '0';
}
}
void convertD3() {
convD3 = 0;
for (int i = 0; i < scrubbedD3.length(); i++) {
convD3 = convD3 * 10 + scrubbedD3[i] - '0';
}
}
void convertDegree() {
if (scrubbedDegree == "SECURITY") {
convDegree = SECURITY;
}
else if (scrubbedDegree == "NETWORK") {
convDegree = NETWORK;
}
else if (scrubbedDegree == "SOFTWARE") {
convDegree = SOFTWARE;
}
else {
std::cout << "ERROR\n";
}
}
void convert() {
convertAge();
convertD1();
convertD2();
convertD3();
convertDegree();
}
int main()
{
cout << "Course Title: Scripting and Programming - Applications - C867\n";
cout << "Language Used: C++\nStudent ID: 011142624\nName: Isaiah Bullock\n";
const string studentData[] = {
"A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY", "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK", "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE", "A4,Erin,Black,[email protected],22,50,58,40,SECURITY", "A5,Isaiah,Bullock,[email protected],21,20,25,30,SOFTWARE"
};
Roster classRosterArray(5);
scrub(studentData[0]);
convert();
classRosterArray.add(scrubbedID, scrubbedFName, scrubbedLName, scrubbedEmail, convAge, convD1, convD2, convD3, convDegree);
scrub(studentData[1]);
convert();
classRosterArray.add(scrubbedID, scrubbedFName, scrubbedLName, scrubbedEmail, convAge, convD1, convD2, convD3, convDegree);
scrub(studentData[2]);
convert();
classRosterArray.add(scrubbedID, scrubbedFName, scrubbedLName, scrubbedEmail, convAge, convD1, convD2, convD3, convDegree);
scrub(studentData[3]);
convert();
classRosterArray.add(scrubbedID, scrubbedFName, scrubbedLName, scrubbedEmail, convAge, convD1, convD2, convD3, convDegree);
scrub(studentData[4]);
convert();
classRosterArray.add(scrubbedID, scrubbedFName, scrubbedLName, scrubbedEmail, convAge, convD1, convD2, convD3, convDegree);
classRosterArray.printAll();
}
学生.cpp
#include <iostream>
#include "degree.h"
#include "student.h"
using std::string;
Student::Student(string iStudentID, string iFName, string iLName, string iEmail, int iAge, int daysToCom1, int daysToCom2, int daysToCom3, DegreeProgram iDegree) {
setID(iStudentID);
setFName(iFName);
setLName(iLName);
setEmail(iEmail);
setAge(iAge);
setComArray(daysToCom1, daysToCom2, daysToCom3);
setDegree(iDegree);
}
void Student::setID(string iStudentID) {
studentID = iStudentID;
}
void Student::setFName(string iFName) {
fName = iFName;
}
void Student::setLName(string iLName) {
lName = iLName;
}
void Student::setEmail(string iEmail) {
email = iEmail;
}
void Student::setAge(int iAge) {
age = iAge;
}
void Student::setComArray(int daysToCom1, int daysToCom2, int daysToCom3) {
numDaysToCom[0] = (daysToCom1);
numDaysToCom[1] = (daysToCom2);
numDaysToCom[2] = (daysToCom3);
}
void Student::setDegree(DegreeProgram iDegree) {
degree = iDegree;
}
string Student::getID() {
return studentID;
}
string Student::getFName() {
return fName;
}
string Student::getLName() {
return lName;
}
string Student::getEmail() {
return email;
}
int Student::getAge() {
return age;
}
int Student::getDay1() {
return numDaysToCom[0];
}
int Student::getDay2() {
return numDaysToCom[1];
}
int Student::getDay3() {
return numDaysToCom[2];
}
double Student::getAvgDays() {
double avg = 0;
for (int i = 0; i < 3; i++) {
avg = avg + numDaysToCom[i];
}
std::cout << avg << " is the average amount of days in the three courses\n";
return avg;
}
DegreeProgram Student::getDegree() {
return degree;
}
void Student::print() {
int i;
std::cout << studentID << "\tFirst Name: " << fName << "\tLast Name: " << lName << "\tAge: " << age << "\tdaysInCourse: {";
for (i = 0; i < 2; i++) {
std::cout << numDaysToCom[i] << ", ";
}
std::cout << numDaysToCom[i] << "} Degree Program: ";
switch (degree) {
case 0:
std::cout << "SECURITY\n";
break;
case 1:
std::cout << "NETWORK\n";
break;
case 2:
std::cout << "SOFTWARE\n";
break;
default:
std::cout << "INVALID DEGREE\n";
}
}
名册.cpp
#include <iostream>
#include "roster.h"
#include "student.h"
#include "degree.h"
Roster::Roster() {
capacity = 0;
size = 0;
Student classRosterArray[1];
}
Roster::Roster(int capacity) {
capacity = capacity;
size = 0;
Student* classRosterArray = new Student[capacity];
}
/*
Roster::Roster(int capacity) {
this->capacity = capacity;
this->size = 0;
this->classRosterArray = new Student * [capacity];
}
*/
Roster::~Roster() {
for (int i = 0; i = size; i++) {
delete this->classRosterArray[i];
}
delete classRosterArray;
}
void Roster::add(string iStudentID, string iFName, string iLName, string iEmail, int iAge, int daysToCom1, int daysToCom2, int daysToCom3, DegreeProgram iDegree) {
classRosterArray[size] = new Student(iStudentID, iFName, iLName, iEmail, iAge, daysToCom1, daysToCom2, daysToCom3, iDegree);
size += 1;
}
void Roster::remove(string studentID) {
for (int i = 0; i < size; i++) {
if (classRosterArray[i]->getID() == studentID) {
delete classRosterArray[i];
}
else {
std::cout << "No student with ID\n";
}
}
}
void Roster::printAll() {
for (int i = 0; i < size; i++) {
(this->classRosterArray)[i]->print();
}
}
void Roster::printAverageDaysInCourse(string studentID) {
for (int i = 0; i < size; i++) {
if (classRosterArray[i]->getID() == studentID) {
classRosterArray[i]->getAvgDays();
}
else {
std::cout << "No student with ID\n";
}
}
}
void Roster::printInvalidEmails() {
for (int i = 0; i < size; i++) {
classRosterArray[i]->getEmail();
}
}
如果有帮助的话我可以包含我的标题,但我真的不认为这会改变任何事情。
我尝试输入其他人的代码,但没有成功。我还尝试将默认构造函数更改为大小为 1,只是为了看看是否允许至少一项内容进入,但这也不起作用。
好吧,关于类和构造函数如何工作的一些严重误解
Roster::Roster() {
capacity = 0;
size = 0;
Student classRosterArray[1];
}
Roster::Roster(int cap) {
capacity = cap;
size = 0;
Student* classRosterArray = new Student[capacity];
}
应该是
Roster::Roster() {
capacity = 0;
size = 0;
array = nullptr;
}
Roster::Roster(int capacity) {
capacity = capacity;
size = 0;
array = new Student[capacity];
}
有一个类似这样的类
class Roster
{
public:
Roster();
Roster(int capacity);
...
private:
int capacity;
int size;
Student* array;
};