i have ran into an unknown problem in C++ when develping my small example with Visual Studio, and after debugging many times, i can't still find out what is the problem here, please help me solve this:
Here is my Header.h:
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
And here is my 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();
};
Now is the implementation for 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);
}
Next, i define Driver class:
// 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();
};
And the implementation for that:
#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());
}
I have a wrapper for Driver and Team:
#pragma once
#include "Driver.h"
#include "Team.h"
#include <algorithm>
using namespace std;
class F1WorldFinal
{
public:
F1WorldFinal();
~F1WorldFinal();
};
And this is the main class:
#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;
}
But after changing the problem line into:
teamRedBull->AddRacer(new Driver("Sebastian Vettel", "German", 42, 4, teamRedBull));
Everything works well.
Can you take time to explain me why this happened, i have debug and it turns out the problem is in xstring @@, so i really can't solve this.