calling constructor of a class in another class

强颜欢笑 提交于 2021-01-29 05:07:37

问题


I have two classes named info and i2 in qt. info is a c++ class and i2 is a Qt designer from class. I want to use i2 as an environment for testing my info class.
I included info.h in the header of i2.my info class has two constructor like below:

info();
info(const void *ip, const char* pw, const void *hostName);

now I want to make an object of info in i2 class and pass those three parameters( ip and pw and hostname) to info and use them in my program. like below:

private:
    Ui::i2 *ui;
    info inf("172.30.6.91", "mypw", "heydari.f");

this doesn't work . I got these errors:

/home/heydari.f/i1/i2.h:48: error: expected identifier before string constant
     info inf("172.30.6.91", "mypw", "heydari.f");
              ^~~~~~~~~~~~~

and :

/home/heydari.f/i1/i2.h:48: error: expected ‘,’ or ‘...’ before string constant

my info class header :

#ifndef INFO_H
#define INFO_H
#include <fstream>
#include <vector>
#include <libssh/libssh.h>

class info
{
private:

    ssh_session my_ssh_session = ssh_new();
    const void* _hostName;
    const void* _ip;
    const char* _password;

public:
    info();
    info(const void *ip, const char* pw, const void *hostName);
    ~info();
    //some another functions

};

#endif // INFO_H#ifndef KERNEL_H

my i2 class header:

#ifndef I2_H
#define I2_H
#include <QDialog>
#include "info.h"

namespace Ui {
class i2;
}

class i2 : public QDialog
{
    Q_OBJECT

public:
    explicit i2(QWidget *parent = 0);
    ~i2();

private slots:
//some push button functios

private:
    Ui::i2 *ui;
    info inf;

};

#endif // I2_H


回答1:


Use a braced-init-list:

info inf{"172.30.6.91", "mypw", "heydari.f"};


来源:https://stackoverflow.com/questions/62964807/calling-constructor-of-a-class-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!