当分配的类动态数组时,代码退出

发布于 2025-01-26 18:47:00 字数 1248 浏览 3 评论 0原文

我正在尝试动态分配一个数组,并且每当它到达动态分配程序退出的部分时。我宁愿在尝试使用动态阵列来学习如何执行此操作时不使用向量。

这是简化的代码:

#include <iostream>
#include <string>
using namespace std;

class Student
{
private:
double calcAverage(double* testArray);
char calcGrade(double average);
public:
int nTests, sameTests, idNum;
string name;
double average, *testArray;
char grade;
};
int i;

Student fillStudentArray(int nStudents);

int main()
{
*studentArray = fillStudentArray(nStudents);
return 0;
}

Student fillStudentArray(int nStudents)
{
Student *newStudentArray = new Student[nStudents];
cout << "If you can see this I worked. ";
delete[] studentArray;
return *newStudentArray;
}

我尝试了在此处发布的解决方案 creation C ++中动态对象的动态阵列,但它也以类似的方式退出。代码的主要内容看起来像这样。

int main()
{
int nStudents = 3; //this number is just for testing, in actual code it has user input
Student** studentArray = new Student*[nStudents];
cout << "1 ";
for(i = 0; i < nStudents; i++)
{
    cout << "2 ";
    studentArray[i] = new Student[25];
    cout << "3 ";
}
return 0;
}

I am trying to dynamically allocate an array and whenever it gets to the part where it dynamically allocates the program exits. I would rather not use vectors as I am trying to learn how to do this using dynamic arrays.

This is the simplified code:

#include <iostream>
#include <string>
using namespace std;

class Student
{
private:
double calcAverage(double* testArray);
char calcGrade(double average);
public:
int nTests, sameTests, idNum;
string name;
double average, *testArray;
char grade;
};
int i;

Student fillStudentArray(int nStudents);

int main()
{
*studentArray = fillStudentArray(nStudents);
return 0;
}

Student fillStudentArray(int nStudents)
{
Student *newStudentArray = new Student[nStudents];
cout << "If you can see this I worked. ";
delete[] studentArray;
return *newStudentArray;
}

I have tried the solution posted here Creation of Dynamic Array of Dynamic Objects in C++ but it also exits in a similar way. The main for the code looks like this.

int main()
{
int nStudents = 3; //this number is just for testing, in actual code it has user input
Student** studentArray = new Student*[nStudents];
cout << "1 ";
for(i = 0; i < nStudents; i++)
{
    cout << "2 ";
    studentArray[i] = new Student[25];
    cout << "3 ";
}
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2025-02-02 18:47:00

关闭(无论如何是雪茄)

Student* fillStudentArray(int nStudents); <<== function must return pointer to students

int main()
{
    int nStudents = 3; <<<=== declared nstudents
    Student *studentArray = fillStudentArray(nStudents); <<< declare studentArray
    return 0;
}

Student *fillStudentArray(int nStudents) <<<== return pointer
{
    Student* newStudentArray = new Student[nStudents];
    cout << "If you can see this I worked. ";
   // delete[] studentArray; <<<== what were you trying to delete?
    return newStudentArray; <<<=== return pointer
}

您显示的第二个代码无关紧要,它创建了一个2D阵列

close (heres a cigar anyway)

Student* fillStudentArray(int nStudents); <<== function must return pointer to students

int main()
{
    int nStudents = 3; <<<=== declared nstudents
    Student *studentArray = fillStudentArray(nStudents); <<< declare studentArray
    return 0;
}

Student *fillStudentArray(int nStudents) <<<== return pointer
{
    Student* newStudentArray = new Student[nStudents];
    cout << "If you can see this I worked. ";
   // delete[] studentArray; <<<== what were you trying to delete?
    return newStudentArray; <<<=== return pointer
}

the second code you showed is not relevant, its creating a 2d array

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文