[英]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: 当我用Visual Studio开发我的小例子时,我遇到了C ++中的一个未知问题,经过多次调试后,我仍然无法找到问题所在,请帮我解决这个问题:
Here is my Header.h: 这是我的Header.h:
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
And here is my Team.h: 这是我的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: 现在是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类:
// 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. 你能不能花点时间解释一下为什么会发生这种情况,我已经调试了,事实证明问题出在xstring @@中,所以我真的无法解决这个问题。
Thanks to WhozCraig, now i have fixed my problem, after using std:: instead of using namespace std. 感谢WhozCraig,现在我已经修复了我的问题,在使用std ::而不是使用命名空间std之后。
Here is my editing version: 这是我的编辑版本:
Driver.h 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 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 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: 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 F1WorldFinal.h
#pragma once
#include "Driver.h"
#include "Team.h"
class F1WorldFinal
{
public:
F1WorldFinal();
~F1WorldFinal();
};
Header.h 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. 谢谢大家,非常感谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.