将char *转换为str奇怪的输出

时间:2017-03-25 17:03:26

标签: c++ string char type-conversion

我是因为当我想将str转换为char *时,我在c ++程序上遇到问题,当我调试它时,我可以看到" this-> nom" = France vs Espagne,它是我想要的...我会在最后向您展示调试代码。 " nom"属性是在Spectacle.h类中,如果你想让它让我知道。

这是我的代码(主要的第一个):

Match m("France", "Espagne", "Football", "Championnat du monde");
m.getTarif() = 110;
m.ajoutDate("Paris", "24/04/2017");
m.ajoutDate("Nantes", "26/08/2017");

std::cout << "Nom :" << m.getNom() << std::endl;

然后是match.h:

#pragma once
#ifndef __Match_h__

#define __Match_h__
#include "Spectacle.h"
#include <string>

class Match : public Spectacle

{
public:
Match(char* equipe1, char* equipe2, char* sport, char* compet);
void ajoutDate(char * lieu, char * date);
char* getType();
char* getEquipe1();
char* getEquipe2();
char* getSport();
char* getTypeCompetition();
~Match();

private:
char* competition;
char* equipe1;
char* equipe2;
char* sport;
};

#endif //__Match_h__

然后是match.cpp:

#include "Match.h"

Match::Match(char * equipe1, char * equipe2, char * sport, char * compet)
{
this->date = new char*[100];
this->lieu = new char*[100];
this->index = 0;
this->type = "Sport";

this->equipe1 = equipe1;
this->equipe2 = equipe2;
this->sport = sport;
this->competition = compet;
char* vs = " VS ";
std::string result;
result += equipe1;
result += vs;
result += equipe2;
char* res = (char*)result.c_str();
this->nom = res;
}

void Match::ajoutDate(char * lieu, char * date)
{
if (this->index < 1)
{
    this->date[this->index] = date;
    this->lieu[this->index] = lieu;
    this->index++;
}
else
{
    std::cout << "Il ne peut pas y avoir 2 dates pour ce concert.";
}
}

char * Match::getType()
{
return this->type;
}

char * Match::getEquipe1()
{
return this->equipe1;
}

char * Match::getEquipe2()
{
return this->equipe2;
}

char * Match::getSport()
{
return  this->sport;
}

char * Match::getTypeCompetition()
{
return this->competition;
}

Match::~Match()
{
}

这是spectacle.cpp:

#include "Spectacle.h"


Spectacle::Spectacle(char* nom)
{

this->date = new char*[100];
this->lieu = new char*[100];
this->nom = nom;
this->index = 0;
this->type = "indefinie";
}

Spectacle::Spectacle()
{
}


Spectacle::~Spectacle()
{
}

void Spectacle::ajoutDate(char * lieu, char * date)
{
if (this->index < 100)
{
    this->date[this->index] = date;
    this->lieu[this->index] = lieu;
    this->index++;
}
else
{
    std::cout << "Il y a déjà 100 dates pour ce concert.";
}
}

char * Spectacle::getLieu(char * date)
{
for (int i = 0; i < this->index; i++) {
    if(this->date[i] == date){
        return this->lieu[i];
    }
}
return "Rien trouver";
}

int & Spectacle::getNbDate()
{
return this->index;
}

float& Spectacle::getTarif()
{
return this->tarif;
}

void Spectacle::synthese()
{
std::cout << "Nom :" << this->nom << ", Type: " << this->type
    << ", Nb dates: " << this->getNbDate() << std::endl;
for (int i = 0; i < this->index; i++) {
    std::cout << this->date[i] << " " << this->lieu[i] << std::endl;
}
}


char* Spectacle::getNom()
{
return this->nom;
}

char * Spectacle::getType()
{
return *this->lieu;
}

这是调试: http://i.imgur.com/1OiWWTs.png

这就是我最后的结论: http://i.imgur.com/ix8CNEr.png

感谢帮助人们,祝你有个美好的一天!

2 个答案:

答案 0 :(得分:0)

无法找到您的数据成员。

this->date = new char*[100];
this->lieu = new char*[100];
this->index = 0;
this->type = "Sport";

您正在调用,但它们不属于Match类。首先修复你的代码错误。

答案 1 :(得分:0)

很抱歉,Spectacle文件不是必需的,因为问题出在Match构造函数中并且正是:

    std::string result;
    result += equipe1;
    result += vs;
    result += equipe2;
    char* res = (char*)result.c_str();
    this->nom = res;

结果是一个局部变量,您的数据成员 nom 指向 result.c_str()指向的对象,因此 nom result.c_str()共享同一个对象,但是当构造函数方法超出范围时, result 变量就消失了,所以object result.c_str()是垃圾,您的数据成员 nom 指向垃圾。 您在调试时可以查看 nom 的正确值的原因是因为您在构造函数范围内进行了调试,因此结果变量仍在堆栈中。

解决方案:此问题有很多解决方案,例如您可以将结果用作类成员,这样您就可以确保它在匹配之前不会消失对象消失了。