简体   繁体   中英

How do I call the constructor of a class' member?

I wanted to declare a QGraphicsView which has a previously declared member as an argument of the constructor, but the compiler interprets it as a function.

(Code for reference)

class Widnow : public QMainWindow
{
    Q_OBJECT

    // constructors, member functions, etc...

private:
    Ui::Widnow *ui;
    QTimer timer01;
    QGraphicsScene gaem;
    QGraphicsView wiev(&gaem); //this gets interpreted as a function
}

Trying to call the constructor as QGraphicsView wiev = QGraphicsView(&gaem); also causes an error, as the copy constructor has been deleted... Is there a way to declare this member without errors?

The C++ compiler thinks that it is a method declaration where "wiev" as method that returns an instance of QGraphicsView. Use this way

class Widnow : public QMainWindow
{
 private:
    QGraphicsScene gaem;
    QGraphicsView wiev; //this gets interpreted as a function

   public:
     Widnow() : wiev(&gaem)
     {}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM