2022.12.7---运算符重载

封装my_string并实现=、+、==、!=、==、>、<、<<、>>等运算符的重载

#include <iostream>
#include<cstring>

using namespace std;

class my_string
{
private:
    char *cstr;
    int len;
public:
    my_string():cstr(NULL),len(0) {/*cout<<"无参构造"<<endl;*/}  //无参构造函数
    my_string(const char *str)    //有参构造函数
    {
        len=strlen(str);
        cstr=new char[len+1];
        strcpy(cstr,str);
        //cout<<"有参构造"<<endl;
    }
    my_string(const my_string &other)    //拷贝构造函数
    {
        this->len=other.len;
        this->cstr=new char[len+1];
        strcpy(this->cstr,other.cstr);
        //cout<<"拷贝构造"<<endl;
    }
    ~my_string()    //析构函数
    {
        if(cstr!=NULL)
        {
            delete cstr;
        }
        cout<<"析构函数"<<endl;
    }

    //运算符重载
    friend istream &operator>>(istream &L,my_string &R);// >>重载
    friend ostream &operator<<(ostream &L,my_string &R);// <<重载
    my_string &operator=(my_string &R)      //运算符重载=
    {
        if(this->cstr!=NULL)
        {
            strcpy(this->cstr,R.cstr);
            this->len=R.len;
        }
        else
        {
            this->cstr=new char[R.len+1];
            strcpy(this->cstr,R.cstr);
            this->len=R.len;
        }
        return *this;
    }
    my_string &operator+(const my_string &R)   //运算符重载+,字符串加字符串
    {
        strcat(this->cstr,R.cstr);
        this->len+=R.len;
        return *this;
    }
    my_string &operator+(const char ch)   //运算符重载+,字符串加字符
    {
        this->len+=1;
        this->cstr[this->len-1]=ch;
        return *this;
    }
    bool operator==(const my_string &R)const    //运算符重载==
    {
        return ~strcmp(this->cstr,R.cstr);
    }
    bool operator!=(const my_string &R)const    //运算符重载!=
    {
        return strcmp(this->cstr,R.cstr);
    }
    bool operator>(const my_string &R)const    //运算符重载>
    {
        return ~strcmp(this->cstr,R.cstr);
    }
    bool operator<(const my_string &R)const    //运算符重载<
    {
        return strcmp(this->cstr,R.cstr);
    }
};

istream &operator>>(istream &L,my_string &R)
{
    if(R.cstr!=NULL)
    {
        L>>R.cstr;
        R.len=strlen(R.cstr);
    }
    else
    {
        R.cstr=new char[20];
        L>>R.cstr;
        R.len=strlen(R.cstr);
    }
    return L;
}
ostream &operator<<(ostream &L,my_string &R)
{
    L<<R.cstr;
    return L;
}

int main()
{
    my_string s;                    //s调用无参构造
    cout<<"请输入一个字符串:";
    cin>>s;                         // >>运算符重载
    cout<<"s: "<<s<<endl;            // <<运算符重载
    my_string s1("helloya");        //s1调用有参构造
    cout<<"s1: "<<s1<<endl;          // <<运算符重载
    my_string s2;                   //s2调用无参构造
    s2=s;                           // =运算符重载
    cout<<"s2=s: "<<s2<<endl;
    if(s1!=s2)                      // !=运算符重载
    {
        cout<<"s1和s2不相等"<<endl;
        if((s1>s2)>0)                   // >运算符重载
        {
            cout<<"s1大于s2"<<endl;
        }else
        {
            cout<<"s1小于s2"<<endl;
        }
    }else
    {
        cout<<"s1和s2相等"<<endl;
    }
    cout<<"s2+'h': "<<s2+'h'<<endl; // +运算符重载
    cout<<"s1+s2: "<<s1+s2<<endl;    // +运算符重载

    return 0;
}

..\..\MainWindow.cpp(24): error C2039: "processButton": 不是 "Ui::MainWindow" 的成员 C:\Users\admin\Desktop\QT\untitled1\build\Desktop_Qt_6_10_0_MSVC2022_64bit-Debug\ui_MainWindow.h(70): note: 参见“Ui::MainWindow”的声明 ..\..\MainWindow.cpp(49): error C2665: “op::PoseExtractorCaffe::PoseExtractorCaffe”: 没有重载函数可以转换所有参数类型 C:\openpose-prosperity\include\openpose/pose/poseExtractorCaffe.hpp(19): note: 可能是“op::PoseExtractorCaffe::PoseExtractorCaffe(const op::PoseModel,const std::string &,const int,const std::vector<op::HeatMapType,std::allocator<op::HeatMapType>> &,const op::ScaleMode,const bool,const bool,const std::string &,const std::string &,const float,const bool,const bool)” ..\..\MainWindow.cpp(49): note: “op::PoseExtractorCaffe::PoseExtractorCaffe(const op::PoseModel,const std::string &,const int,const std::vector<op::HeatMapType,std::allocator<op::HeatMapType>> &,const op::ScaleMode,const bool,const bool,const std::string &,const std::string &,const float,const bool,const bool)”: 无法将参数 6 从“op::Point<int>”转换为“const bool” ..\..\MainWindow.cpp(55): note: 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符 ..\..\MainWindow.cpp(49): note: 尝试匹配参数列表“(op::PoseModel, std::string, int, initializer list, op::ScaleMode, op::Point<int>, op::Point<int>)”时 ..\..\MainWindow.cpp(87): error C3861: “displayImage”: 找不到标识符 ..\..\MainWindow.cpp(90): error C2440: “初始化”: 无法从“void”转换为“cv::Mat” ..\..\MainWindow.cpp(90): note: void 类型的表达式不能转换为其他类型 ..\..\MainWindow.cpp(93): error C3861: “displayImage”: 找不到标识符 ..\..\MainWindow.cpp(97): error C2556: “cv::Mat MainWindow::processFrame(cv::Mat &)”: 重载函数与“void MainWindow::processFrame(cv::Mat &)”只是在返回类型上不同 C:\Users\admin\Desktop\QT\untitled1\MainWindow.h(28): note: 参见“MainWindow::processFrame”的声明 ..\..\MainWindow.cpp(97): error C2371: “MainWindow::processFrame”: 重定义;不同的基类型 C:\Users\admin\Desktop\QT\untitled1\MainWindow.h(28): note: 参见“MainWindow::processFrame”的声明 ..\..\MainWindow.cpp(166): error C2039: "displayImage": 不是 "MainWindow" 的成员 C:\Users\admin\Desktop\QT\untitled1\MainWindow.h(12): note: 参见“MainWindow”的声明 cl -c -nologo -Zc:wchar_t -FS -Zc:rvalueCast -Zc:inline -Zc:strictStrings -Zc:throwingNew -permissive- -Zc:__cplusplus -Zc:externConstexpr -permissive- -Zi -MDd -std:c++17 -utf-8 -W3 -w34100 -w34189 -w44456 -w44457 -w44458 -wd4577 -wd4467 -EHsc /Fddebug\untitled1.vc.pdb -DUNICODE -D_UNICODE -DWIN32 -D_ENABLE_EXTENDED_ALIGNED_STORAGE -DWIN64 -DQT_QML_DEBUG -DQT_MULTIMEDIA_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I..\..\..\untitled1 -I. -IC:\openpose-prosperity\include -IC:\opencv\build\include -I"C:\Program Files\Microsoft Visual Studio\18\Community\VC\Tools\MSVC\14.44.35207\include" -IC:\Qt\6.10.0\msvc2022_64\include -IC:\Qt\6.10.0\msvc2022_64\include\QtMultimedia -IC:\Qt\6.10.0\msvc2022_64\include\QtWidgets -IC:\Qt\6.10.0\msvc2022_64\include\QtGui -IC:\Qt\6.10.0\msvc2022_64\include\QtNetwork -IC:\Qt\6.10.0\msvc2022_64\include\QtCore -Idebug -I. -I/include -IC:\Qt\6.10.0\msvc2022_64\mkspecs\win32-msvc -Fodebug\ @C:\Users\admin\AppData\Local\Temp\moc_MainWindow.obj.7824.922.jom moc_MainWindow.cpp jom: C:\Users\admin\Desktop\QT\untitled1\build\Desktop_Qt_6_10_0_MSVC2022_64bit-Debug\Makefile.Debug [debug\MainWindow.obj] Error 2 jom: C:\Users\admin\Desktop\QT\untitled1\build\Desktop_Qt_6_10_0_MSVC2022_64bit-Debug\Makefile [debug] Error 2 00:52:55: The command "C:\Qt\Tools\QtCreator\bin\jom\jom.exe" terminated with exit code 2. 00:52:55: Error while building/deploying project untitled1 (kit: Desktop Qt 6.10.0 MSVC2022 64bit) 00:52:55: The kit Desktop Qt 6.10.0 MSVC2022 64bit has configuration issues which might be the root cause for this problem. 00:52:55: When executing step "Make" 00:52:55: Elapsed time: 00:06.
11-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值