C++ 我的C++;项目类数组不';无法工作,程序崩溃而没有错误报告
我正在做一个小的实践项目来帮助我的类和对象。 编译时,我没有收到任何错误,运行程序时,我能够输入第一个值(如“main()”函数中所示,但在此之后,程序崩溃时没有错误报告,除了“进程终止状态为-1073741819” 该程序基于我所在学院的结构。我的目的是输入数组的大小(是的,我知道主函数中没有“cout”行提示您输入数组的大小) 这是我的课程:C++ 我的C++;项目类数组不';无法工作,程序崩溃而没有错误报告,c++,C++,我正在做一个小的实践项目来帮助我的类和对象。 编译时,我没有收到任何错误,运行程序时,我能够输入第一个值(如“main()”函数中所示,但在此之后,程序崩溃时没有错误报告,除了“进程终止状态为-1073741819” 该程序基于我所在学院的结构。我的目的是输入数组的大小(是的,我知道主函数中没有“cout”行提示您输入数组的大小) 这是我的课程: #include <iostream> #include <string> using namespace std; cl
#include <iostream>
#include <string>
using namespace std;
class Module
{
private:
string Module_Name; //pretty self-explainaroty
int Assignment_1_Total_Points; //total tally of Assignment 1 Points
int Assignment_2_Total_Points; //total tally of Assignment 2 Points
int Assignment_1_Max; //Max possible points for Assignment 1
int Assignment_2_Max; //Max possible points for Assignment 2
int Assignment_1_Question_Nr[]; //individual points of Assignment 2 Questions
int Assignment_2_Question_Nr[]; //individual points of Assignemnt 2 Questions
float Assignment_1_Percentage; //individual Mark for the first Assignment
float Assignment_2_Perventage; //individual Mark for the second Assignment
float Semester_Final_Percentage; //Final Percentage for the Semester Mark
public:
string name; //input variable for Module_Name
int n=0; //number of questions in Assignment 1,
int m=0; //number of questions in Assignment 2
int TA1=0; //total points for Assignment 1
int TA2=0; //total points for Assignemnt 2
int MA1=0; //Max points for Assignemnt 1
int MA2=0; //Max points for Assignment 2
int A1QP=0; //input variable to store in Member Variable Array
int A2QP=0; //input variable to store in Member Variable Array
int SumA1=0; //Sum of points to be stored
int SumA2=0;
Module() //constructor
{
Module_Name="";
Assignment_1_Total_Points=0;
Assignment_2_Total_Points=0;
Assignment_1_Max=0;
Assignment_2_Max=0;
Assignment_1_Question_Nr[n]={};
Assignment_2_Question_Nr[m]={};
}
void Set_Module_Name()//input the Modlue name and make Module_Name equal to it
{
cout<<"Enter the Name of the Module: ";
getline(cin,name);
Module_Name=name;
}
void set_Assignment_Question_Sizes() //setting 'n' and 'm' to input values
{
cout<<"Enter the number of Questions in Assignment 1: ";
cin>>n;
cout<<"Enter the number of Questions in Assignemnt 2: ";
cin>>m;
}
void Get_Set_Max_Points() //setting the maximum Points possible for each assignment
{
cout<<"Enter the Maximum possible Points for Assignment 1: ";
cin>>MA1;
cout<<"Enter the Maximum possible Points for Assignment 2: ";
cin>>MA2;
Assignment_1_Max=MA1; //set the Member Variable so that it cannot be changed later on
Assignment_2_Max=MA2; //set the Member Variable so that it cannot be changed later on
}
void Get_Set_Assignment_Question_Points() //function to get the individual Question Marks
{
for(int i=0;i<n;i++) //Input for Question 1
{
cout<<"Enter the Points given for Assignment 1 Question "<<i+1<<": ";
cin>>A1QP;
Assignment_1_Question_Nr[i]=A1QP; //storing the Question marks in the Member Variable Array
}
for(int j=0;j<m;j++) //Input for Question 2
{
cout<<"Enter the Points given for Assignment 2 Question "<<j+1<<": ";
cin>>A2QP;
Assignment_1_Question_Nr[j]=A1QP; //storing the Question marks in the Member Variable Array
}
}
void Set_Assignment_Total() //getting the Sum Total of the given Marks for both Assignments
{
for(int i=0;i<n;i++)
{
SumA1+=Assignment_1_Question_Nr[i];
}
Assignment_1_Total_Points=SumA1; //storing the Sum of Assignment 1 in the Member Variable
for(int j=0;j<m;j++)
{
SumA2+=Assignment_2_Question_Nr[j];
}
Assignment_2_Total_Points=SumA2; //storing the Sum of Assignment 2 in the Member Variable
}
void Get_Assignment_Marks() //calculating the individual percentages of each assignment
{
Assignment_1_Percentage=(Assignment_1_Total_Points/Assignment_1_Max)*100;
Assignment_2_Perventage=(Assignment_2_Total_Points/Assignment_2_Max)*100;
}
void Show_Details() //Outputs the Module Name, Assignment 1 Mark, Assignment 2 Mark and the Total Mark
{
cout<<"Module Name:\t\t "<<Module_Name<<endl;
cout<<"Assignment 1 Mark: \t"<<Assignment_1_Percentage<<endl;
cout<<"Assignment 2 Mark: \t"<<Assignment_2_Perventage<<endl;
cout<<"Final Mark: \t\t"<<Semester_Final_Percentage<<endl;
}
void Run_Time() //combining all the required member functions into a single function
{
Set_Module_Name();
set_Assignment_Question_Sizes();
Get_Set_Max_Points();
Get_Set_Assignment_Question_Points();
Set_Assignment_Total();
Get_Assignment_Marks();
cout<<"\n\n\n";
Show_Details();
}
};
但是,当我尝试使用以下代码时,它会崩溃:
int main()
{
int x;
cin>>x;
for(int i=0;i<x;i++)
{
Module Module[x];
Module[x].Run_Time();
}
return 0;
}
intmain()
{
int x;
cin>>x;
对于(inti=0;i来说,您似乎希望在这里创建动态表,但它不起作用
int Assignment_1_Question_Nr[];
int Assignment_2_Question_Nr[];
如果要创建这样的表,应该使用指针
int* Assignment_1_Question_Nr;
int* Assignment_2_Question_Nr;
Module() //constructor
{
...
Assignment_1_Question_Nr = new int[n];
Assignment_2_Question_Nr = new int[m];
}
void set_Assignment_Question_Sizes() //setting 'n' and 'm' to input values
{
cout << "Enter the number of Questions in Assignment 1: ";
cin >> n;
delete[] Assignment_1_Question_Nr;
Assignment_1_Question_Nr = new int[n];
cout << "Enter the number of Questions in Assignemnt 2: ";
cin >> m;
delete[] Assignment_2_Question_Nr;
Assignment_2_Question_Nr = new int[m];
}
~Module() //destructor
{
delete[] Assignment_1_Question_Nr;
delete[] Assignment_2_Question_Nr;
}
崩溃时正在执行哪一行?另外,创建一个int赋值_1_问题_Nr[];
是否真正编译?它不应该:Module-Module[x];
创建数组。Module[x]。运行时间()你可以很幸运地看到它的崩溃。你很幸运地看到它崩溃了。在你最喜欢的C++书籍中阅读更多关于数组。谢谢。我会检查这些问题。如果你想创建这样的表,你应该使用指针——不,它们应该使用<代码> STD::向量< /代码>。@ PaulMcKeNeNeYes,我的坏。但是创建表W在创建模块后,指针将以特定的大小进行操作。但即使是您的代码也是错误的,因为它使用了错误的delete
。它应该是delete[]
。此外,出现内存泄漏,因为当模块
超出范围时内存未释放。@PaulMcKenzie我不久前意识到了这一点,我正在研究内存泄漏的解决方案并编辑我的答案,但我的IDE不配合。但是谢谢。@PaulMcKenzie好的,我想现在应该没事了。
int* Assignment_1_Question_Nr;
int* Assignment_2_Question_Nr;
Module() //constructor
{
...
Assignment_1_Question_Nr = new int[n];
Assignment_2_Question_Nr = new int[m];
}
void set_Assignment_Question_Sizes() //setting 'n' and 'm' to input values
{
cout << "Enter the number of Questions in Assignment 1: ";
cin >> n;
delete[] Assignment_1_Question_Nr;
Assignment_1_Question_Nr = new int[n];
cout << "Enter the number of Questions in Assignemnt 2: ";
cin >> m;
delete[] Assignment_2_Question_Nr;
Assignment_2_Question_Nr = new int[m];
}
~Module() //destructor
{
delete[] Assignment_1_Question_Nr;
delete[] Assignment_2_Question_Nr;
}
int main()
{
Module module;
module.Run_Time();
}