我用一个字符串构造了一个C ++构造函数,但是它不会打印出字符串

时间:2018-10-01 09:29:58

标签: c++ string constructor

下面是我做的一个名为TheSecondVeryBestClassEverMade的类(我已经做过以前有史以来最好的类,所以这就是为什么!)做了一个构造函数,并在参数中做了一个字符串z。在此构造函数的主体中,我介绍了另一个函数(在C ++中是一个函数,在Java中是方法?),并将字符串名称设置为z。

此后,我从TheSecondVeryBestClassEverMade中创建了一个对象,并在其参数中创建了一个字符串,内容为:“曼波5号是我的果酱!” (因为这是一首很棒的歌曲吧!!),但是现在这些令人毛骨悚然的东西不会在终端中打印出来。

我在这里做什么错了?

这是我的c ++代码:

#include <iostream>
#include <string>

using namespace std;

//Constructor is a function that gets called automatically when a object is made.
// No more explicitly calling of the function, does it automatically!

class TheSecondVeryBestClassEverMade{
    public:
    //Constructors never have a return type so nothing gets returned in the body. 
    // Constructor name == as the class name:
    TheSecondVeryBestClassEverMade(string z){
        //DONT print anything out in a constructor, only IDIOTS do that.
        // Are you a idiot? No don't think so buddy!
        //Normally its used to give variables a initial value. EXAMPLE TIME:
        setTheSecondVeryBestName(z);
    }
    void setTheSecondVeryBestName(string bb){
        name = bb;
    }
    string getTheSecondVeryBestName(){
        return name;
    }

private:
    string name;
};

int main() {
    TheSecondVeryBestClassEverMade bodyOder("Mambo Number 5 is my jam baby!");

    return 0;
}

1 个答案:

答案 0 :(得分:2)

您制作了if (!categoriesTree.parent)) { categoriesTree.parent = {'name': '', 'children': []}; } categoriesTree.parent['children'].push({'name': category.name, 'id': category.id}); ,并在其构造函数中给了它一个字符串,然后您对其进行了正确设置...但是您没有任何实际可打印它的代码。添加类似

bodyOder

然后在建立void printName(){ std::cout << "Name: " << name << std::endl; } 之后调用它:

bodyOder