簡體   English   中英

Visual Studio C ++中的運行時錯誤

[英]Runtime error in Visual Studio C++

當我用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類:

// 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 @@中,所以我真的無法解決這個問題。

感謝WhozCraig,現在我已經修復了我的問題,在使用std ::而不是使用命名空間std之后。

這是我的編輯版本:

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>

主要

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

謝謝大家,非常感謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM