Error 1 error C2512: 'Date' : no appropriate default constructor available

I am having issues developing my classes for a project. I am getting error C2512: "Date" no appropriate default constructor available. I am by no means an experienced programmer and I have taken upon myself to learn C++. That being said I am stuck at this.

This is my main
#include <iostream>
#include <string>
using namespace std;

#include "Budget.h"

int main()
{
string userPayDate ;// Variable to hold weeklyDate
Date myDate1 ;

cout << "Do you get paid weekly? (Y/N)" << endl;
getline( cin, userPayDate );
myDate1.setWeeklyDate( userPayDate );
if ( userPayDate == "Y" || "y");
cout << "So you get paid once a week?" <<endl;
if ( userPayDate == "N" || "n" );
cout << "So you get paid once every 2 weeks?" <<endl;

return 0;
This is my class declarations

#include <iostream>
#include <string>
using std::string;


class Date
{
public:
Date ( string ) ;//Declaration of the constructor for the Date class
/*~Date(){} ;//Declaration of the deconstructor for the Date class*/
void setWeeklyDate (string) ;// Declaration of setPayDate member function
string getWeeklyDate () ;//declaration of function the receives payDate

private:
string weeklyDate;//weeklyDate for this Date
};//end class Date

This is my class member functions

#include <string>
using std::string;


#include "Budget.h"

Date::Date ( string date )
{
weeklyDate = date;
}

void Date::setWeeklyDate( string date )
{

weeklyDate = date;
}//setting weeklyDate ending setWeeklyDate functions


string Date::getWeeklyDate()
{
return weeklyDate;
}


Any insight into what I am doing wrong would be greatly appreciated

Thanks
Last edited on
If you write a class constructor which takes parameters (which you have Date ( string ) ; ), then you are also responsible for providing the default
constructor which takes no parameters Date::Date() which you have not provided).
So you cannot have the statement:
1
2
3
4
int main()
{
string userPayDate ;// Variable to hold weeklyDate
Date myDate1 ; //ERROR - cannot do this without a default constructor 




You can cure this in one of two ways:

1. Modify the exiting constrctor to have a default value:
1
2
3
4
class Date
{
public:
Date ( string date =  "10 Jan 2010") ; //or whatever format string you use for your date. 



2. Provide a seperate default constructor that takes no parameters.


I would do option 1 myself.
So when I created the constructor in my class declarations ( Date ( string ) ; ) I gave it an argument type ( string ). So when I am trying to create an object of that class myDate1 it is not finding the constructor because I am not specifying the class constructor/or because this is different from what I designed the constructor to handle. I get twisted around the logic; so when you say that you would prefer method 1...why is that? Just curious as to why this would be better.

Thanks
I suggest method 1 as all it requires is just giving the existing constructor's parameter a default value.
Last edited on
Topic archived. No new replies allowed.