我正在做一个项目,每次我去设置 example[4].m_dArray[3] 时,程序都会崩溃。我可以设置所有其他变量的值,直到到达 example[4].m_dArray[3]。任何帮助,将不胜感激!
Prog1Class.h:
#include "Prog1Struct.h"
#pragma once
#include <iostream>
#include <string.h>
using namespace std;
class Prog1Class
{
private:
Prog1Struct example[4];
public:
Prog1Class();
~Prog1Class ();
void setStructData();
void getStructData();
void ptrFunction();
void refFunction();
void printStruct();
void printData();
};
Prog1Struct.h:
#pragma once
#include <string.h>
struct Prog1Struct {
int m_iVal;
double m_dArray[4];
char m_sLine[80];
};
Prog1Class.cpp:
#include "Prog1Class.h"
#include "Prog1Struct.h"
#include <iostream>
#include <string.h>
using namespace std;
Prog1Class::Prog1Class()
{
}
Prog1Class::~Prog1Class()
{
delete &example[4];
}
int main()
{
Prog1Class *aClass = new Prog1Class();
aClass->setStructData();
aClass->printData();
return 0;
}
void Prog1Class::setStructData()
{
for(int i=0;i<5;i++)
{
cout << "Enter an integer: ";
cin >> example[i].m_iVal;
for(int j=0;j<5;j++)
{
cout << endl << "Enter a double: ";
cin >> example[i].m_dArray[j];
}
cout << endl << "Enter a string: ";
cin.ignore(256,'\n');
cin.getline(example[i].m_sLine, 80, '\n');
cout << endl;
}
}
void Prog1Class::getStructData()
{
}
void Prog1Class::printData()
{
for(int i=0;i<5;i++)
{
cout << example[i].m_iVal;
for(int j=0;j<5;j++)
{
cout << example[i].m_dArray[j];
}
cout << example[i].m_sLine;
}
}