zoukankan      html  css  js  c++  java
  • c++ templat乱测

    该上机实验环境 linux mint  IDE:qt5.11   代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串

    在linux mint下可以运行,测试目的:检验复制构造函数以及左移运算符在输出类对象方面的作用

    #include <iostream>
    #include<string.h>
    using namespace std;
    
    class mycoach
    {
    public:
        friend ostream & operator<<(ostream& out,mycoach&t);
        mycoach()
        {
            age=22;
            c_name=new char[1];
            strcpy(c_name,"");
        }
    
        mycoach(char * name,int age)
        {
            this->age=age;
            c_name=new char[strlen(name)+1];//never forgot allowa space
            strcpy(c_name,name);
        }
    
        mycoach(const mycoach &t)
        {
            this->age=t.age;
            strcpy(this->c_name,t.c_name);
        }
    
        ~mycoach()
        {
            if(c_name!=NULL)
            {
                delete [] c_name;
                c_name=NULL;
            }
        }
    
        mycoach& operator=(const mycoach& t)
        {
            if (c_name!=NULL)
            {
                delete[] c_name;
                age=22;
                c_name=NULL;
            }
            c_name=new char[strlen(t.c_name)+1];
            strcpy(c_name,t.c_name);
            age=t.age;
            return *this;//this is a pointer *this  is value
        }
    
        void print()
        {
            cout<<"hello~emma "<<this->c_name<<" emma "<<this->age<<" years old";
        }
    private:
        char*name[32];
        char * c_name;
        int age;
    };
    
    ostream & operator<<(ostream& out,mycoach&t)
    {
        out<<"大家好~我是:"<<t.c_name<<",今年"<<t.age<<endl;
    }
    
    int main()
    {
        //
        mycoach cpc("陈培昌",22);
        mycoach fgf;
        fgf=cpc;
        cout<<fgf<<endl;
        mycoach fgf2("付高峰",30);
        fgf=fgf2;
        cout<<fgf<<endl;
        cout << "Hello World!" << endl;
        return 0;
    }

  • 相关阅读:
    Windows&Android&ios后台机制总结
    启动Mongodb服务
    js获取一个月有多少天
    NaN类型和数字相加为NaN
    sql将datetime类型与字符串进行比较
    存储过程拼接sql
    将list转换成用逗号连接的字符串
    sql在查询结果集上新增一列
    让两个div排列在一行
    返回不同数据库类型的IDBConnection
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/12079262.html
Copyright © 2011-2022 走看看