-7

The constructor without parameters work but the other one does not. I'm very desperate, I tried everything

// header

 class Etudiant
        {
        private:
            char * name;
            unsigned int age;
            Date *datenaissance;
        public:
            Etudiant();
            Etudiant(char * c,unsigned int,Date&);
            ~Etudiant();
        };

this is my .cpp

    Etudiant::Etudiant()
    {
        this->name = new char();
        strcpy(name, "kabil");
        this->age = 18;

    this->datenaissance = new Date();
}

Etudiant::Etudiant(char * c, unsigned int a, Date &d)
{
    this->name = new char();
    strcpy(this->name,c);
    this->age = a;
    this->datenaissance = new Date(d);
}


Etudiant::~Etudiant()
{
    delete[]name;
    name = 0;
}

this is my main

int main()
{

    Date d();   
    Etudiant E(),E1("student",15,d);

    system("pause");

}

what should I change?

solomonk
  • 3
  • 2
  • 4
    Use `std::string`. And replace `Date d();` by `Date d;` otherwise you declare a function. – Matthieu Brucher Dec 29 '18 at 22:55
  • `Date d();` -- If you're expecting this to declare a `Date` object, it doesn't. This does not do what you think it does. – PaulMcKenzie Dec 29 '18 at 22:57
  • where should i add it? – solomonk Dec 29 '18 at 22:58
  • 3
    @solomonk -- Don't just "add it". Remove all of that `char *` stuff and replace it with `std::string`. – PaulMcKenzie Dec 29 '18 at 22:58
  • 3
    `this->name = new char();` here you allocate a single character. – drescherjm Dec 29 '18 at 22:59
  • i can't do that , i need to work with char * first – solomonk Dec 29 '18 at 22:59
  • 1
    Pretend `new` doesn't exist. Pretend `char *` doesn't exist. Use `std::string` for all string data. Declare your `Date` object correctly. – PaulMcKenzie Dec 29 '18 at 23:00
  • i changed to this->name = new char[strlen(c)+1]; , now how do i fix the last problem? – solomonk Dec 29 '18 at 23:01
  • Using `std::string` won't fix all the problems though. – juanchopanza Dec 29 '18 at 23:01
  • 2
    Why is `datenaissance` a pointer? `name=0` should be `name=nullptr` although it's useless (C++ classes in France are usually bad :( ). – Matthieu Brucher Dec 29 '18 at 23:05
  • 1
    @PaulMcKenzie seriously? Memory management *IS* a part of C++. You can't pretend that it doesn't exist and skip learning it. Of course, for production level code, `new` should not exist. But for learning purposes you cannot avoid it. – Yksisarvinen Dec 29 '18 at 23:12
  • @MatthieuBrucher if its not a pointer it will create another datenaissance class no? – solomonk Dec 29 '18 at 23:18
  • 1
    @Matthieu `s/in France/everywhere/` –  Dec 29 '18 at 23:25
  • 2
    @Yksisarvinen -- I did not state it is not a part of C++. The issue is when and where to use dynamic memory management. The OP's code doesn't need any calls to `new`. Second, having assignments given to students like this is one main reason why C++ gets dropped, and those very same students gravitate to Java or similar languages -- it is frustrating to not get the simplest programs to work correctly, when there is absolutely no need for that frustration. Also [Stop teaching C when you're supposed to teach C++](http://cppcast.com/2015/10/kate-gregory/) – PaulMcKenzie Dec 29 '18 at 23:56
  • 1
    Also, if dynamic memory management should be taught, then for heaven's sake, have the student create a `string` or `vector` class, instead of strewing calls to `new` all over the place incoherently. At least when building your own container classes, you get the idea of proper, sane, and coherent uses of `new` and `delete`. – PaulMcKenzie Dec 30 '18 at 00:00
  • 2
    The funny thing I see a lot is that the student states "they can't use `std::string`", but nevertheless sneak in usage of one or more `std::string` variables in their code, *and the teacher accepts the code* anyway. That shows that a lot of this `char *`-only is a farce. – PaulMcKenzie Dec 30 '18 at 00:22
  • Outstanding. Am I to follow "Do not feed"-signs? – greybeard Dec 30 '18 at 05:46

1 Answers1

1

To pass literal string to a function, it must have a parameter of type char const *, not char *. So your constructor should have this prototype:

Etudiant(char const * c, unsigned int, Date &);

Saying the above, you also do not allocate enough memory to copy a string in your constructor. This line:

this->name = new char();

should likely be:

this->name = new char[strlen(c) + 1];

so you have enough memory for this copy operation:

strcpy(this->name, c);
Tomek
  • 4,554
  • 1
  • 19
  • 19
  • 1
    Please don't encourage people to use `new` (especially without mentioning `delete`) manually. – πάντα ῥεῖ Dec 29 '18 at 23:07
  • 2
    @solomonk You shouldn't use that code. Use `std::string`. – πάντα ῥεῖ Dec 29 '18 at 23:08
  • Unfortunately, this seems to be an assignment. I've witnessed too much bad C++ teaching in France not to feel sympathetic towards @solomonk. – Matthieu Brucher Dec 29 '18 at 23:10
  • @MatthieuBrucher yes :/ im doing what my teacher is doing, can you tell me what documentation should i follow that teaches c++ professionally? – solomonk Dec 29 '18 at 23:12
  • @Tomek in `Etudiant(char * c, unsigned int a, Date &d)`, `d` should be a `const&`. – Matthieu Brucher Dec 29 '18 at 23:12
  • Have a look at https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Matthieu Brucher Dec 29 '18 at 23:12
  • 4
    Downvoted for not explicitly discouraging C-style strings and `operator new[]`. This is terrible practice which should never go unmentioned for a beginner audience. – Baum mit Augen Dec 29 '18 at 23:24
  • @solomonk _"im doing what my teacher is doing, can you tell me what documentation should i follow that teaches c++ professionally?"_ Regarding the documentation you can follow https://en.cppreference.com , this is quite complete and up to date. It's widely seen in education that c++ is taught in bad manners. Whatsoever, questions like yours aren't really useful for future and professional research, because production level code won't ever see such silly restrictions that you _can't use `std::string`_. – πάντα ῥεῖ Dec 29 '18 at 23:36
  • Sometimes you do find cases where the employer requires no `string`, no `vector`, etc.... Sometimes this is because of some sort of resource constraint, but what it usually means is they want the program written in C. – user4581301 Dec 30 '18 at 00:09
  • @user4581301 In such cases using `new` is rarely an option as well. I've been involved with embedded programming for decades. – πάντα ῥεῖ Dec 30 '18 at 00:22
  • True. Everything "dynamic" will likely be preallocated and stored in freelists until needed. I've probably gone years at a time without a using `malloc`. I can count the number of times I've used `new` in a C program on the toes on my hands. – user4581301 Dec 30 '18 at 00:32