【发布时间】:2019-04-11 17:37:37
【问题描述】:
我有点困惑为什么这不打印名字! 我有一个human.cpp:
#include <string>
#include <iostream>
#include "human.h"
human::human(int age, human *n){
m_age=age;
name = new char[2];
human::~human() = default;
void human::printDetails(){
std::cout <<"name is " << name << " age is " << m_age << std::endl;
}
和human.h:
class human {
public: //: needed
human(int age, human *name);
~human();
void printDetails();
private :
char *name;
int m_age;
};
最后是 main.cpp:
#include <iostream>
#include <string>
#include "human.h"
int main()
{
human *Alex = new human(10, Alex); //pointer // needs argument //should have both age and name
Alex->printDetails(); //print not Print
}
所以我的问题是:它打印年龄,但不打印姓名?有什么建议?谢谢:)
【问题讨论】:
-
human::~human() = default;是什么?它有什么作用? -
你不应该构建的代码。请阅读how to ask good questions 和this question checklist。最后学习如何创建Minimal, Complete, and Verifiable Example。
-
更糟糕的是,您可能对作业以及您应该做什么有一些误解。我的猜测是你应该将“人”的名字作为字符串传递给构造函数。
-
你有Java背景吗?