Please I need some help here.

Hi,I`m a new C++ programmer, and i`m stuck with this error:

#include <iostream>
#include <string>
#include "order.h"
#include "date.h"


Order::Order(Date check_in, Date check_out,int owner_id,int order_id){ [Error: No Default constructor exists for class "Date" ]
_check_in=check_in;
_check_out=check_out;
_owner_id=owner_id;
_order_id=order_id;
_num_of_nights=((_check_out._year * 372) + (_check_out._month -1) * 31);
}

Order::Order(Date check_in,int num_of_nights,int owner_id,int order_id){
_check_in=Date(check_in);
_check_out::_day = _check_in._day + num_of_nights; //nos5at 5ishov ta2re5 yetsi2a
_num_of_nights=num_of_nights;
_owner_id=owner_id;
_order_id=order_id;
}

#include <iostream>
#include <string>

#ifndef _DATE_H_
#define _DATE_H_

using namespace std;

class Date{
public:
Date(int year,int month,int day);
string to_string();
private:

void print_date();
int _year;
int _month;
int _day;
string stringDate;
};
#endif

Compiling Error:
error C2512: 'Date' : no appropriate default constructor available.

Please i need some help.
Thnx
Last edited on
You need to use an initialization list here, otherwise the default (parameterless) constructor will be invoked for all your date objects - and you don't have such a constructor. The appropitiate way to do this is:

1
2
3
4
5
Order::Order(Date check_in, Date check_out,int owner_id,int order_id) :
_check_in(check_in), _check_out(check_out), _owner_id(owner_id), _order_id(order_id), _num_of_nights((_check_out._year * 372) + (_check_out._month -1) * 31)
{

}
i still have the same problem, but it move to :
Order::Order(Date check_in, Date check_out,int owner_id,int order_id) :
_check_in(check_in), _check_out(check_out), _owner_id(owner_id), _order_id(order_id), _num_of_nights((_check_out._year * 372) + (_check_out._month -1) * 31)
{ [Error: No Default constructor exists for class "Date" ]


}
closed account (z05DSL3A)
error C2512: 'Date' : no appropriate default constructor available.

Create an appropriate default constructor.

1
2
3
4
5
6
7
class Date
{
public:
    Date(int year = 1970, int month = 1, int day = 1);
    string to_string();
private:
    //... 
Last edited on
Wouldn't the problem rather be that he has yet another Date field in his class that is not initialized in the constructor?
closed account (z05DSL3A)
Hanst, I be honest, that code hurts my eyes and there is nothing that stands out as being wrong. Maybe with more code, neatly formatted, it would be easier to see what is going on.


Aymanbah,
Please use code tags: [code] Your code here [/code]
I be honest, that code hurts my eyes

I see what you mean.
I`m sorry, but it`s my first "program" on C++, and i`m learing it by my self, so please take this in consideration.
Grey Wolf suggestion solved the problem.
thnx guys.
Last edited on
closed account (z05DSL3A)
There is nothing to be sorry about.

Welcome to the forum.
Last edited on
Topic archived. No new replies allowed.