C ++类无法打印?

时间:2018-11-08 08:27:45

标签: c++ class

我有点困惑为什么它没有印出名字! 我有一个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

    }

所以我的问题是:它显示年龄,但不显示名称?有什么建议么?谢谢:)

4 个答案:

答案 0 :(得分:4)

您的代码中不需要任何 new 。由于您在代码中 #include d ,所以我假设您要使用它:

  #include 
#include 

类人
{
    年龄
    std :: string名称;

上市:
    人(int age,std :: string name)
    :年龄{age},
      姓名{姓名}
    {}

    int get_age()const {返回年龄; }
    std :: string const&get_name()const {返回名称; }

    void print_details()const {
        std :: cout <<“我的名字是” << name <<“。我是” << age <<“岁。\ n”;
    }
};

int main()
{
    人p {19,“ Alex”};
    p.print_details();
}
 

如果您*真的*想用困难的方式 tm

  #include  // std :: strlen()
#include <实用程序> // std :: exchange(),std :: swap()
#include 

类人
{
    字符* name_;
    int age_;

上市:
    Person(int age,char const * name)//构造函数
                       //我们不想在nullptr上调用std :: strlen()
                       //而是只分配一个char并将其设置为'\ 0'。
    :name_ {new char [name? std :: strlen(name)+1:1] {}},
      age_ {age}
    {
        如果(名字)
            std :: strcpy(name_,名称);
    }

    Person(Person const&other)//复制构造函数
    :name_ {new char [std :: strlen(other.name_)+ 1]},
      age_ {other.age_}
    {
        std :: strcpy(name_,other.name_);
    }

    Person(Person && other)noexcept //移动构造函数
    :name_ {std :: exchange(other.name_,nullptr)},//因为other将是
      age_ {other.age_} //还是浪费了,我们
    {} //“窃取”其资源

    Person&运算符=(其他人)noexcept //复制分配运算符
    {//由于参数other得到了
        std :: swap(name_,other.name_); //复制并将被破坏
        age_ = other.age_; //在函数的末尾
        返回* this; //可以简单地交换指针
    } //-称为copy&swap习语。

    〜Person(){delete [] name_; } //析构函数

    void print_details()常量
    {
        std :: cout <<“我是” << name_ <<“。我是” << age_ <<“岁。\ n”;
    }
};

int main()
{
    人p {19,“ Alex”};
    p.print_details();
}
 

如果您不想实现特殊的成员函数,则必须 = delete; ,这样它们才能生成由编译器生成的版本-对于管理其自身资源的类而言,该版本将无法正常工作-不会被意外致电。

答案 1 :(得分:0)

我想您对human构造函数的第二个参数感到困惑。看一下这些变化:

human.h

class human {
public:
    human(int age, char *name);
    human(const human& h);
    human& operator=(const human& h);
    void printDetails();
    virtual ~human();
private:
    int age;
    char *name;
};

human.cpp

human::human(int _age, char *_name) {
    age = _age;
    name = new char[strlen(_name)+1];
    strcpy(name, _name);
}

human::human(const human& _h) { //Copy constructor
    age = _h.age;
    name = new char[strlen(_h.name)+1];
    strcpy(name, _h.name);
}

human& human::operator=(const human& _h) { //Copy assignment operator
    age = _h.age;
    name = new char[strlen(_h.name)+1];
    strcpy(name, _h.name);
    return *this;
}

void human::printDetails(){
    std::cout <<"name is " << name << " age is " << age << std::endl;
}

human::~human() { //Destructor
    delete[] name;
}

main.cpp

int main() {

    human *alex = new human(10, "Alex");
    alex->printDetails();

    human *anotherAlex = new human(*alex);
    anotherAlex->printDetails();

    delete alex;
    delete anotherAlex;
}

建议:我将使用Human作为类名,使用alex作为变量名。 (看看我如何将它们大写)

答案 2 :(得分:0)

    #include <iostream>
    #include <cstring>
    #include <new>

    using namespace std;

    class human {
        public: 
            human(int age, const char * name)
            {
                m_age=age;
                m_name = new char[strlen(name)+1];
                strcpy(m_name,name);
            }
            ~human()
{
delete[] m_name;
}
            void printDetails()
            {
                std::cout <<"name is      " << m_name << " age is " << m_age << std::endl;
            }
        private :
            char *m_name;
            int m_age;

    };

    int main()
    {

        human *Alex = new human(10, "alex"); //pointer // needs argument //should have both age and name
        Alex->printDetails(); //print not Print
        delete Alex;
        return 0;
    }

您需要阅读更多内容。您共享的示例在许多层面上都是错误的,还请阅读有关动态内存管理的信息。

答案 3 :(得分:-1)

对于初学者,您可以使用std :: string,因此您已经包含了它。 ))

human.h

#include <string>
class human
{
    public: //: needed
        human(int age, std::string name);
        void printDetails();
    private :
        std::string name;
        int m_age;
};

human.cpp

#include <iostream>
#include "human.h"

human::human(int age, std::string n)
{
    m_age = age;
    name = n;
}

void human::printDetails()
{
    std::cout <<"name is: " << name << " age is: " << m_age << std::endl;
}

main.cpp

#include "human.h"

int main()

{
    human *Alex = new human(10, "Alex");
    Alex->printDetails();

}