C++で新たに開始しましたが、かなり厄介な問題が残っています。私は動的配列を使用するとすぐに私は立ち往生しています。配列は実際にはデバッグ(表示画像)に乱れていて、アレイに1つ多くのオブジェクトを追加するとクラッシュします。これは私が1つの特定のプロジェクトを取得するエラーではありませんが、動的配列を使用するすべてのコードは、私もこのコースで先生によって作られたコードをコンパイルしようとしたが成功しませんでした。だから、おそらくそれは問題ではなく、おそらく他のコードである可能性は低いです。しかし、私はこれを証明するために使用したテストコードを安全に含めるだけでした。 配列が最初のインデックスを超えてクラッシュする
#include "iostream"
#include "string"
#include "Student.h"
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
string input;
Student **students = NULL;
students = new Student*[20];
for(int i = 0; i < 20; i++)
{
students[i] = new Student();
}
for(int i = 0; i < 20; i++)
{
delete students[i];
}
delete[] students;
return 0;
}
#include "Student.h"
#include "string"
Student::Student()
{
name = "";
number = 0;
}
Student::Student(string Name)
{
name = Name;
number = 0;
}
Student::~Student()
{
}
string Student::getName() const
{
return name;
}
#ifndef STUDENT_H
#define STUDENT_H
#include "string"
#include "Course.h"
using namespace std;
class Student
{
private:
string name;
int number;
public:
Student();
Student(string Name);
virtual ~Student();
string getName() const;
};
#endif
生徒を配列に追加することはありません。 Studentオブジェクトをどのように配列に入れるのかという質問を更新してください – Attila
'std :: vector'やその他のSTLコンテナの使用を検討するべきです。 – mfontanini
あなたのテストでは、NULLポインタが指す値を検査できないことが実証されています。驚くべきことは何ですか? –