Having difficulty creating operator overload for class module using literal string as argument

Viewed 33

I am relatively new to C++. I am hoping to get assistance creating an overload operator that will permit me to use a literal string with a class I am writing. This project is for learning.

I created a custom Date class (and have not worried about the actual logic in the class; I'm just trying to get the interface working and to understand where my roadblock is.

It may not look like it, but I have put in a lot of time on this, so an explanation designed for a beginning c++ progammer would be very helpful to me and anyone who follows in my footsteps. Basically, I'd like to be able to do this with my class:

Date dt(5,6,92);//create class object

int myint;
myint = dt;//this works
string mystr;
mystr = dt("mm/dd/yy");//this does not compile

Thanks in advance. (A compilable test program shown below)

#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

class Date
{
    int mo, da, yr;

    public:
    Date(int m, int d, int y)
    {
        cout << "Constructor int m, int d, int y called " << endl;
        mo = m; da = d; yr = y;
    }


string getdatestr(const char *s = "") {//user sends in format "d2/" "mm/dd/yy" etc
cout << "getdatestr const char *s called mo = " << mo << " da = " << da << "yr = " << yr << endl;
return to_string(mo) + "/" + to_string(da) + "/" + to_string(yr);
}

int getdate(){
cout << "getdate int t called" << endl;
string tag;
tag = to_string(yr) + to_string(mo) + to_string(da);

return stoi(tag);
}

string getdate(string &str){
cout << "getdate with string as ref" << endl;
return "5/5/55";
}

int getdate(int &myint){
cout << "getdate with int as ref" << endl;
return 12345;
}

void setdate(string dt){
cout << "setdate string dt called " << dt << endl;
     mo = 1;
     da = 2;
    yr = 2020;
  }

void setdate(int intdte){
cout << "setdate int to " << intdte << endl;
mo = 99;//any int for now
da = 98;
yr = 1997;
}

void setdate(const char *dte){
cout << "setdate char* dte = " << dte << endl;
mo = 94;
da = 95;
yr = 1996;
}

~Date(){
cout << "destructor called" << endl;
}

//below code permits this in main(): myint = dt;
 operator int() {
     cout << "operator int()" << endl;
    string tag;
tag = to_string(yr) + to_string(mo) + to_string(da);
return stoi(tag);
     }
//end section to permit: myint = dt;


//what do I need so I can do this:  mystr = dt("mm/dd/yy");, nothing below worked; I tried many more iterations
//than what are shown here
/*
operator string() {
cout << "operator string char" << endl;
return "hello world";
}

string operator = (string &rhs){
 cout << "string" << endl;
return "return a string";
}

operator = (const string &rhs){
cout << "will this work?" << endl;
return *this;
}

char& operator = (char(&)[9]){
cout << "whoa, it worked" << endl;
//return "got it";
}

operator = (char*){
cout << "try again" << endl;
}

string operator const char(&)[9] {
cout << "one more time" << endl;
string *ptr;
ptr = "one more time";
return ptr;
}
//end nothing worked to permit me to do this mystr = dte("mm/dd/yy"); section
*/
};//end of class


int main()
{
//create a Date class object dt
Date dt(5, 6, 92);

dt.setdate("02/15/22");

cout << endl;
cout << "next two mystr messages return null because I " << endl;
cout << "can't seem to write a valid overload operator for a literal string" << endl;
string mystr;
//mystr = dt("mm/dd/yy");//does not compile (no match for call to '(Date) (const char [9])'
cout << "mystr using dt(str) = " << mystr << endl;

string myconv = "mm/dd/yy";
//mystr = dt(myconv);//does not compile (no match for call to '(Date) (std::__cxx11::string&)'
cout << "mystr using dt(mm//dd//yy) = " << mystr << endl;
cout << endl;


//this section works
//can I assign dt to an integer (as #days from a reference date)
cout << "this section seems to work" << endl;
int myint;
cout << "myint = dt;" << endl;
 myint = dt;//does not compile
cout << "myint (using  = dt;) = " << myint << endl;
cout << endl;

system("pause");
}
0 Answers
Related