首页 > 解决方案 > 使用文字字符串作为参数为类模块创建运算符重载时遇到困难

问题描述

我对 C++ 比较陌生。我希望获得创建重载运算符的帮助,该运算符将允许我在正在编写的类中使用文字字符串。这个项目是为了学习。

我创建了一个自定义 Date 类(并且不担心类中的实际逻辑;我只是想让界面正常工作并了解我的障碍在哪里。

它可能看起来不像,但我在这方面投入了很多时间,所以为初级 c++ 程序员设计的解释对我和任何追随我脚步的人都会非常有帮助。基本上,我希望能够在我的课堂上做到这一点:

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

提前致谢。(下图是一个可编译的测试程序)

#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");
}

标签: c++operator-overloading

解决方案


推荐阅读