简体   繁体   中英

Runtime error in Visual Studio C++

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.

Thanks to WhozCraig, now i have fixed my problem, after using std:: instead of using namespace std.

Here is my editing version:

Driver.h

#pragma once
#include "Header.h"

class Team;

class Driver
{
private:
    std::string hoTen;
    std::string quocTich;
    int soLanThang;
    int soLanVoDich;
    std::string tenTeam;
    static int diem;
public:
    Driver();

    Driver(std::string hoTen, std::string quocTich, int soLanThang, int soLanVoDich, Team* tenTeam);
    ~Driver();
};

Team.h

#pragma once
#include "Header.h"
class Driver;

class Team
{
private:
    std::string quocGia;
    std::string ten;
    int soLanVoDich;
    std::vector<Driver*> ds_nguoi_dua;
public:
    Team();
    Team(std::string quocGia, std::string ten, int soLanVD);
    ~Team(); 
    void AddRacer(Driver* myD);

    void AddRacer(std::string ten, std::string quocTich, int slThang, int slVD, Team* tenTeam);
    std::string getName();
};

Driver.cpp

#include "Driver.h"

int Driver::diem = 0;

Driver::Driver()
{
    this->hoTen = "";
    this->quocTich = "";
    this->soLanThang = 0;
    this->soLanVoDich = 0;
}

Driver::~Driver()
{
}

class Team{
public:
    std::string getName();
};

Driver::Driver(std::string mHoTen, std::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());
}

Team.cpp:

#include "Team.h"


Team::Team()
{
}

Team::Team(std::string ten, std::string quocGia, int soLanVD){
    this->quocGia = quocGia;
    this->ten = ten;
    this->soLanVoDich = soLanVD;
}

std::string Team::getName(){
    return this->ten;
}


Team::~Team()
{
}

void Team::AddRacer(Driver* myD){
    this->ds_nguoi_dua.push_back(myD);
}

class Driver{
public:
    Driver(std::string myTen, std::string quocTich, int slThang, int slVD, Team* tenTeam);
};

void Team::AddRacer(std::string myTen, std::string quocTich, int slThang, int slVD, Team* tenTeam){
    Driver* myD = new Driver(myTen, quocTich, slThang, slVD, this);
    this->ds_nguoi_dua.push_back(myD);
}

F1WorldFinal.h

#pragma once
#include "Driver.h"
#include "Team.h"

class F1WorldFinal
{
public:
    F1WorldFinal();
    ~F1WorldFinal();
};

Header.h

#pragma once
#include <iostream>
#include <vector>
#include <string>

main

#include "F1WorldFinal.h"


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;
}

Thank you all, i'm very appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM