Visual Studioで小さなサンプルをデベロップして何度もデバッグしても、ここで問題が見つかっていません。この:ここVisual Studio C++でランタイムエラーが発生しました
は私HEADER.Hです:
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
そして、ここでは私のTeam.hです:
#pragma once
#include "Header.h"
class Driver;
class Team
{
private:
string quocGia;
string ten;
int soLanVoDich;
vector<Driver*> ds_nguoi_dua;
public:
Team();
Team(string quocGia, string ten, int soLanVD);
~Team();
void AddRacer(Driver* myD);
void AddRacer(string ten, string quocTich, int slThang, int slVD, Team* tenTeam);
string getName();
};
は今Team.hのための実装です:
#include "Team.h"
Team::Team()
{
}
Team::Team(string ten, string quocGia, int soLanVD){
this->quocGia = quocGia;
this->ten = ten;
this->soLanVoDich = soLanVD;
}
string Team::getName(){
return this->ten;
}
Team::~Team()
{
}
void Team::AddRacer(Driver* myD){
this->ds_nguoi_dua.push_back(myD);
}
class Driver{
public:
Driver(string myTen, string quocTich, int slThang, int slVD, Team* tenTeam);
};
void Team::AddRacer(string myTen, string quocTich, int slThang, int slVD, Team* tenTeam){
Driver* myD = new Driver(myTen, quocTich, slThang, slVD, this);
this->ds_nguoi_dua.push_back(myD);
}
次に、私は、ドライバクラスを定義します。
// Driver.h
#pragma once
#include "Header.h"
class Team;
class Driver
{
private:
string hoTen;
string quocTich;
int soLanThang;
int soLanVoDich;
string tenTeam;
static int diem;
public:
Driver();
Driver(string hoTen, string quocTich, int soLanThang, int soLanVoDich, Team* tenTeam);
~Driver();
};
そして、そのための実装:
#include "Driver.h"
int Driver::diem = 0;
Driver::Driver()
{
this->hoTen = "";
this->quocTich = "";
this->soLanThang = 0;
this->soLanVoDich = 0;
}
Driver::~Driver()
{
}
class Team{
public:
string getName();
};
Driver::Driver(string mHoTen, string mQuocTich, int soLanThang, int slVD, Team* team){
this->hoTen.assign(mHoTen);
this->quocTich.assign(mQuocTich);
this->soLanThang = soLanThang;
this->soLanVoDich = slVD;
this->tenTeam.assign(team->getName());
}
私はドライバーとチームのラッパーを持っている:
#pragma once
#include "Driver.h"
#include "Team.h"
#include <algorithm>
using namespace std;
class F1WorldFinal
{
public:
F1WorldFinal();
~F1WorldFinal();
};
そして、これはメインクラスです:
#include <cstdlib>
#include <iostream>
#include "F1WorldFinal.h"
using namespace std;
int main(){
Team* teamRedBull = new Team("Red Bull", "Austrian", 4);
Driver* driver = new Driver("David Coulthard", "British", 13, 0, teamRedBull);
teamRedBull->AddRacer(driver);
// The problem happens here.
teamRedBull->AddRacer("Sebastian Vettel", "German", 42, 4, teamRedBull);
return 0;
}
しかしに問題のある行を変更した後:
teamRedBull->AddRacer(new Driver("Sebastian Vettel", "German", 42, 4, teamRedBull));
すべてがうまく動作します。
なぜこのようなことが起こったのか説明してください。デバッグがあり、問題がxstring @@になっていることが分かります。本当に解決できません。
このランタイムエラーメッセージのテキストは何ですか?デバッガはあなたに何を伝えますか? –
'new'の使用をやめるか、少なくとも減らしてください。例えば、 'main'が' new'を使う必要はありません。 C++はJavaではありません。さらに、あなたが投稿したコードの残りの部分には 'new'は必要ありません。単に値オブジェクトを作成することができます。 – PaulMcKenzie
関連していないので、あなたはこれを読むために長期的な支持を得ます:[なぜ "名前空間stdを使用して"悪い習慣とみなされていますか?]](https://stackoverflow.com/questions/1452721/why-is-using -namespace-std-consider-bad-practice) – WhozCraig