我在用Visual Studio开发我的小例子时遇到了一个未知问题,经过多次调试后,我仍然无法找到问题所在,请帮我解决这个问题:
这是我的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 @@中,所以我真的无法解决这个问题。
答案 0 :(得分:0)
感谢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;
}
谢谢大家,我非常感激。