类内类的动态数组
我正在尝试构建一个类,其中包含另一个类的对象的动态数组。基本外部类是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过该行,
您可以获取临时地址。该对象将立即被销毁,并且您将指向一个无效的对象。你需要以某种方式制作一个永久的物体。例如:
只是不要忘记删除它。
更好的是——根本不要使用这样的原始数组。使用像 std::vector 这样的容器。
With the line
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:
Just don't forget to delete it.
Even better -- don't use primitive arrays like this at all. Use a container like std::vector.