鱼C论坛

 找回密码
 立即注册
查看: 530|回复: 1

下面的代码错在哪

[复制链接]
发表于 2024-1-9 19:19:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;
class base
{
        base(string m_name, int m_age=0)
        {
                name = m_name;
                age = m_age;
                cout << "base的构造函数" << endl;
        }
        base(const base& p)
        {
                cout << "base的复制构造函数" << endl;
        }
        ~base()
        {
                cout << "base的析构函数" << endl;
        }
        virtual void print()
        {
                cout << age<< endl;
        }

protected:
        string name;
        int age;
};
class base1 :public base
{
public:
        base1(string m_name, int m_age, int m_id);
        virtual void print();
        base1(const base1& p1)
        {
                cout << "base1的复制构造函数" << endl;
        }
        ~base1()
        {
                cout << "base1的析构函数" << endl;
        }
       
private:
        int id;
};
base1::base1(string m_name, int m_age, int m_id) :base(m_name, m_age = 0)
{
        id = m_id;
}
void base1::print()
{
        cout << age << "  " << id << endl;
}

int main()
{


}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-9 19:19:51 | 显示全部楼层
代码中的错误在于缺少头文件的引入和构造函数的定义。

需要引入头文件iostream来使用cout和endl:

#include <iostream>

同时,构造函数的定义应该在类的外部进行:

base::base(string m_name, int m_age=0)
{
    name = m_name;
    age = m_age;
    cout << "构造函数被调用" << endl;
}

另外,还需要在构造函数的参数列表中添加访问修饰符public来使构造函数可被外部调用:

class base
{
public:
    base(string m_name, int m_age=0);
    //其他成员变量和方法
};

注意,这里只是对代码中存在的错误进行了修正,并没有提及其他可能存在的问题。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-6-20 03:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表