类内类的动态数组

发布于 2024-12-10 10:02:30 字数 2150 浏览 0 评论 0原文

我正在尝试构建一个类,其中包含另一个类的对象的动态数组。基本外部类是 Sport,次要类(在数组中)是 Player。我在使添加功能正常工作时遇到了一些问题,现在我终于让它工作了(我想)我从显示器上收到错误。当我调用属于 Player 类一部分的 Display 函数时,出现读取错误。我将在这里发布最大的一段代码,如果有人注意到我出错了,请尽快告诉我。我需要尽快完成这项工作,这项作业早已过期,未来的作业将有助于在此基础上进行。我不仅需要一个工作版本,我还需要了解出了什么问题。

#include "Sport.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

// since we're going to call these from within the class
// consider making all but DisplayMenu() private

Sport::Sport() : array(0),length(0)
{

}

Sport::~Sport()
{

}

void Sport::DisplayMenu()
{
    bool exit(false);
    char entry(0);

    while(exit != true) // clarity
    {
    cout << "\nOREGON INSTITUTE OF TECHNOLOGY\n" << endl;
    cout << "  A - Add Player" << endl;
    cout << "  S - Search/Display a Player" << endl;
    cout << "  D - Display all Players" << endl;
    cout << "  C - Display Current Count of Players" << endl;
    cout << "  E - Exit/n" << endl;
    cin >> entry;

    switch (entry)
    {
        case 'A' :
            Add();
            break;

        case 'S' :
            Search();
            break;

        case 'D' :
            List();
            break;

        case 'C' :
            cout << "Currently " << length << " Players.";
            break;

        case 'E' :
            exit = true;
            break;

        default :

            break;
    }

}
}

void Sport::Add() //have array[] and length
{
Player **temp = new Player *[length+1];

for (int i = 0; i < length; i++)
{
    temp[i] = array[i];
}
temp[length] = &PromptUser();
length++;

delete [] array;
array = temp;
}

void Sport::List()
{
for (int i = 0; i < length; i++)
    (*array)[i].Display(); // <---this line is crashing the program.
}

void Sport::Search() const
{

}

Player Sport::PromptUser()
{
char name[25];
cout << "Enter name: ";
cin >> name;

int grade(0);
cout << "Enter grade: ";
cin >> grade;

double gpa(0.0);
cout << "Enter gpa: ";
cin >> gpa;

Player result(name, grade, gpa);
return result;
}

I am trying to build a class with a dynamic array of another class' objects within it. The base outer class is Sport, the secondary (inside in an array) is Player. I had some problems getting the add function to work and now that I finally have it working (I thought) I am getting errors from the display. When I call the Display function which is part of the Player class I am getting a read error. I'll post the biggest piece of code here and if anyone notices wtf i going wrong please let me know asap. I need tog et this working asap, the assignment is long past due and future assignments help build on top of it. I don't just need a working version I need to understand what is going wrong.

#include "Sport.h"
#include <iostream>

using std::cout;
using std::endl;
using std::cin;

// since we're going to call these from within the class
// consider making all but DisplayMenu() private

Sport::Sport() : array(0),length(0)
{

}

Sport::~Sport()
{

}

void Sport::DisplayMenu()
{
    bool exit(false);
    char entry(0);

    while(exit != true) // clarity
    {
    cout << "\nOREGON INSTITUTE OF TECHNOLOGY\n" << endl;
    cout << "  A - Add Player" << endl;
    cout << "  S - Search/Display a Player" << endl;
    cout << "  D - Display all Players" << endl;
    cout << "  C - Display Current Count of Players" << endl;
    cout << "  E - Exit/n" << endl;
    cin >> entry;

    switch (entry)
    {
        case 'A' :
            Add();
            break;

        case 'S' :
            Search();
            break;

        case 'D' :
            List();
            break;

        case 'C' :
            cout << "Currently " << length << " Players.";
            break;

        case 'E' :
            exit = true;
            break;

        default :

            break;
    }

}
}

void Sport::Add() //have array[] and length
{
Player **temp = new Player *[length+1];

for (int i = 0; i < length; i++)
{
    temp[i] = array[i];
}
temp[length] = &PromptUser();
length++;

delete [] array;
array = temp;
}

void Sport::List()
{
for (int i = 0; i < length; i++)
    (*array)[i].Display(); // <---this line is crashing the program.
}

void Sport::Search() const
{

}

Player Sport::PromptUser()
{
char name[25];
cout << "Enter name: ";
cin >> name;

int grade(0);
cout << "Enter grade: ";
cin >> grade;

double gpa(0.0);
cout << "Enter gpa: ";
cin >> gpa;

Player result(name, grade, gpa);
return result;
}

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

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

发布评论

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

评论(1

ぽ尐不点ル 2024-12-17 10:02:30

通过该行,

temp[length] = &PromptUser();

您可以获取临时地址。该对象将立即被销毁,并且您将指向一个无效的对象。你需要以某种方式制作一个永久的物体。例如:

temp[length] = new Player(PromptUser());

只是不要忘记删除它。

更好的是——根本不要使用这样的原始数组。使用像 std::vector 这样的容器。

With the line

temp[length] = &PromptUser();

you are taking the address of a temporary. That object will immediately be destroyed and you'll be left pointing to an invalid object. You need to make a permanent object somehow. For example:

temp[length] = new Player(PromptUser());

Just don't forget to delete it.

Even better -- don't use primitive arrays like this at all. Use a container like std::vector.

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