I have done a class who contains itself a array of an other class. When I edit one member of this other class and want to print it, it print something not interpretable.
There is the class in file.hpp:
class channel
{
char name[50];
public:
channel();
void setName(char*);
char *getName();
}
class Case
{
char name[50];
channel *channels;
public:
Case();
channel get_channel(int);
}
and there is the file.cpp:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "file.hpp"
channel::channel(){}
void channel::setName(char *c)
{
char buffer[50];
sprintf(buffer, "%s", c);
sprintf(name, "%s", buffer);
}
char* channel::getName()
{
return nom;
}
Case::Case()
{
int i;
channels = (channel *)malloc(14 * sizeof(channel));
for (i = 0; i < 14; i++)
{
channels[i] = channel();
}
}
channel Case::get_channel(int nb)
{
return channels[nb];
}
void main()
{
Case b1 = Case();
b1.get_channel(4).setName("food");
printf("%s", b1.get_channel(4).getName());
}
So I already tried to modify the way to modify the member name with strcpy() or sprintf() and none of this worked.
I also tried to allocate statically like this in file.hpp:
Channel Channels[14]
and dynamically like above but it didn't resolved the problem.