I'm because i have a problem on a c++ programm when i want to convert str to char*, when i debug it, i can see that the "this->nom" = France vs Espagne and it's what i want... i will show you the debug code at the end. The "nom" attribute is in the class Spectacle.h if you want it let me know.
here is my code( main first ) :
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;
then the 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__
then the 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()
{
}
Here is the 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;
}
Here is the debug : https://i.stack.imgur.com/mXKby.png
And here is what i have at the end : https://i.stack.imgur.com/1E31i.png
Thanks for the help guys, have a good day btw !