搜索内容

有一个问题?

如果您有任何疑问,可以在下面询问或输入您要寻找的!

能生成成功,却无法显示内容??应该怎么修改代码

技术聚客 未结 2 642
zhzbd
zhzbd 2021-03-08 00:00
//Information.h #include <iostream> using namespace std; class Employee { private: char *m_name; char *m_no; public: Employee(char *name,char *no); ~Employee() {delete []m_name; delete []m_no;}; void Display();}; class Leader:public Employee { private: char *m_posdes; public: Leader(char name[50],char no[50],char posdes[50]):Employee( name,no){strcpy_s(m_posdes,50,posdes);}; ~Leader() { delete []m_posdes;} void Display();}; //Information.cpp #include"stdafx.h" #include"Information.h" #include<iostream> using namespace std; Employee::Employee( char *name,char *no) { m_name=new char [50]; m_no =new char [50]; strcpy_s(m_name,50,name); strcpy_s ( m_no,50,no); } void Employee::Display() { cout<<"员工的姓名"<<m_name<<endl; cout<<"员工编号"<<m_no<<endl;} void Leader::Display() { Employee::Display(); cout<<"员工职务为"<<m_posdes<<endl; //8.1.1.cpp #include "stdafx.h" #include"Information.h" #include<iostream> using namespace std; int main() { Employee *pe=new Leader("张三","1011","软件开发部部门经理"); pe->Display(); delete pe; system("pause"); return 0; }
相关标签:
2条回答
  • 2021-03-08 00:18

    你的第17行strcpy_s(m_posdes,50,posdes);请问你的m_posde初始化在哪里?没有初始化怎么能拷贝呢,能生成不代表运行不会报错啊。加个初始化应该就可以了。

    	Leader(char name[50], char no[50], char posdes[50]) :Employee(name, no) { m_posdes=new char[50]; strcpy_s(m_posdes, 50, posdes); };
    0 讨论(0)
  • 2021-03-08 00:22
    class Leader :public Employee
    {
    private:
    	char *m_posdes;
    public:
    	Leader(char name[50], char no[50], char posdes[50]) :Employee(name, no){ m_posdes = new char[50]; strcpy_s(m_posdes, 50, posdes); };
    	~Leader()
    	{
    		delete[]m_posdes;
    	}
    	void Display();
    };
    

    m_posdes 没有初始化变量

    0 讨论(0)
提交回复