m0_63133201 2022-03-29 20:57 采纳率: 87%
浏览 438
已结题

"const char *" 类型的默认实参与 "char *" 类型的形参不匹配 要怎么改呢

Game.h
#pragma once
class person
{
    int  m_id;
    const char* m_name;
    int     m_score;
    int  m_money;
private:
    static int m_count;
    static int m_averagescore;
public:
    person(int id, char* name);
    static int getcount();
    static int getaveragescore();
    void setplayer(int id, char* name);
    void showplayer();
    void chaegemoney(unsigned int money);
    void usemoney(unsigned int money);
    ~person() { delete[]m_name; };
};


玩家.cpp
#include<iostream>
using namespace std;
#include"Gamer.h"
person::person(int id = 0,char * name = "default")
{
    m_id = id;
    m_score = 0;
    m_money = 0;
    m_count++;
    m_name = name;
}

int person::m_count = 0;
int person::m_averagescore = 0;
int person::getcount()
{
    return m_count;
}

int person::getaveragescore()
{
    return m_averagescore;
}

void person::setplayer(int id, char* name)
{
    m_id = id;
    m_name = name;
}

void person::showplayer()
{
    cout << "id:[" << m_id << "]\n" << "name:[" << m_name << "]\n"
        << "score:[" << m_score << "]\n" << "money:[" << m_money << "]\n"
        << "cout:[" << m_count << "]\n" << "averagescore:[" << m_averagescore << "]" << endl;
}

void person::chaegemoney(unsigned int money)
{
    m_money += money;
}

void person::usemoney(unsigned int money)
{
    m_money -= money;
    m_score = money;//积分获取方式,累计消费
    m_averagescore += (m_score / m_count);//所有玩家得分平均值
}

int main()
{
    person _A, _B;
    _A.setplayer(4, "course04");
    _A.chaegemoney(100);
    _A.usemoney(90);
    _A.showplayer();

    _B.setplayer(44, "course044");
    _B.chaegemoney(444);
    _B.usemoney(111);
    _B.showplayer();


    system("pause");
    return 0;
}


不知道怎么改,help

img


img

  • 写回答

1条回答 默认 最新

  • 关注

    C++建议用string代替C字符数组,用char指针有很多雷,比如你的析构函数,字符串的内存不是你new出来的,你这么delete,会有问题,如果new出内存,strcpy字符串,那为了不产生浅拷贝,你需要写拷贝构造和拷贝赋值函数,或者禁用这两者。如果不用析构delete,那意味着你必须保证所有传入的都是字面量字符串,程序存在期间永远存在,不能通过交互gets得到的字符串传递,否则name可能会来回的变,或者保证此字符串只用一次。

    #pragma once
    #include <string>
    class person
    {
        int m_id;
        std::string m_name;
        int m_score;
        int m_money;
    
      private:
        static int m_count;
        static int m_averagescore;
    
      public:
        explicit person(int id = 0, const std::string &name = "default");
        static auto getcount() -> int;
        static auto getaveragescore() -> int;
        void setplayer(int id, const std::string &name);
        void showplayer();
        void chaegemoney(unsigned int money);
        void usemoney(unsigned int money);
        ~person() = default;
    };
    
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::string;
    
    person::person(int id, const string &name)
    {
        m_id = id;
        m_score = 0;
        m_money = 0;
        m_count++;
        m_name = name;
    }
    
    int person::m_count = 0;
    int person::m_averagescore = 0;
    auto person::getcount() -> int
    {
        return m_count;
    }
    
    auto person::getaveragescore() -> int
    {
        return m_averagescore;
    }
    
    void person::setplayer(int id, const string &name)
    {
        m_id = id;
        m_name = name;
    }
    
    void person::showplayer()
    {
        cout << "id:[" << m_id << "]\n"
             << "name:[" << m_name << "]\n"
             << "score:[" << m_score << "]\n"
             << "money:[" << m_money << "]\n"
             << "cout:[" << m_count << "]\n"
             << "averagescore:[" << m_averagescore << "]" << endl;
    }
    
    void person::chaegemoney(unsigned int money)
    {
        m_money += static_cast<int>(money);
    }
    
    void person::usemoney(unsigned int money)
    {
        m_money -= static_cast<int>(money);
        m_score = static_cast<int>(money);     //积分获取方式,累计消费
        m_averagescore += (m_score / m_count); //所有玩家得分平均值
    }
    
    int main()
    {
        person A;
        person B;
        person C;
        A.setplayer(4, "course04");
        A.chaegemoney(100);
        A.usemoney(90);
        A.showplayer();
    
        B.setplayer(44, "course044");
        B.chaegemoney(444);
        B.usemoney(111);
        B.showplayer();
        C = B;
        system("pause");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月8日
  • 已采纳回答 3月31日
  • 修改了问题 3月29日
  • 创建了问题 3月29日

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联