简体   繁体   English

如何调用类成员的构造函数?

[英]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.我想声明一个QGraphicsView ,它有一个先前声明的成员作为构造函数的参数,但编译器将它解释为一个函数。

(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);试图将构造函数调用为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. C++ 编译器认为它是一个方法声明,其中“wiev”作为返回 QGraphicsView 实例的方法。 Use this way用这种方式

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何在类的成员函数中调用复制构造函数? - How do you call the copy constructor within a member function of a class? 如何调用基类构造函数? - How do I call the base class constructor? 当我希望调用基类构造函数时,如何在派生类中初始化成员const引用? - How can I initialize a member const reference in a derived class, when I wish to call a base class constructor? 如何从另一个函数调用属于类成员的函数? - How do I call a function that is member of a class, from another function? 如何使用模板调用内部类的构造函数? - c++ How do i call a constructor of an interior class with templates? 如何定义派生类的非成员构造函数(在类标题中) - How do I define the non-member constructor of a derived class (within class header) 如何为具有std :: stringstream成员的类编写复制构造函数? - How do I write a copy constructor for my class which has a std::stringstream member? 如何使用除初始化列表构造函数以外的其他内容为类成员设置默认值 - How do I set a default value for a class member with something other than its initializer list constructor 如何在构造函数初始值设定项列表中初始化std :: unique_ptr对象的类成员std :: vector? - How do I initialize a class member `std::vector` of `std::unique_ptr` object in the constructor initializer list? 您如何在类外部的成员函数中调用成员函数? - How do you call a member function in a member function outside of a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM