Can't initialize variable from Qt framework

I've created a Qt Application with its default structure (a main.cpp file, as well as mainwindow.h, mainwindow.cpp and mainwindow.ui).

The default public attributes in mainwindow.h are:

1
2
3
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();


To which I added two:

1
2
    QTimer *timer;
    QTime time(0, 1, 0);


However, I get a "expected identifier before numeric constant" error:
1
2
     QTime time(0, 1, 0);
                ^


Why am I getting this error and what would be the right way to call the constructor? I'm positive this constructor can take 3 arguments (https://doc-snapshots.qt.io/qt5-5.11/qtime.html).
Thanks.
Last edited on
Try a different name than "time". Maybe it's getting confused with the standard libraries time() function. That doesn't make much sense, but I can't think of anything else based on what you've posted.
Last edited on
Didn't help :(
Thanks for the suggestion, though.
You can't specify parameters like that in the declaration of the class.
1
2
3
4
5
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
     QTimer *timer;
     QTime time;


Then, in the constructor of this class, you can create the time variable with parameters.
Do you have something essentially similar to Foo:
1
2
3
4
5
6
7
class Foo {
public:
  explicit Foo(int = 42);
  ~Foo();
  int* bar;
  std::vector<double> gaz( 7, 3.14 ); // error
};

The constructor and destructor were there already, and do not complain.

You did add a member variable "bar", that happens to be (1) pointer and (2) uninitialized.

You did add member "gaz" too, but is it a variable or a function?
If it is variable, is the (7, 3.14) a proper initialization syntax for a member?
If it is a function, why its parameters are typeless values?

Please show the implementation of the constructor.
It works when you declare it like this:
1
2
3
4
5
6
7
QTime t;

and in your ctor
t.setHMS(1,1,1);

this works too:
QTime t{1,1,1,};
Actually, if I get an error if I try to initialize in the constructor (which is the MainWindow, as my application only has a window and a label so far):

1
2
3
4
5
6
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow){
    ui->setupUi(this);
    time=new QTime(0, 1, 0);
    timer=new QTimer();


Of course, as QTime is not a pointer (as I declared in my header file), I get it's not possible to do this. So I try the following:

1
2
3
4
5
6
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow){
    ui->setupUi(this);
    time(0, 1, 0);
    timer=new QTimer();


And then I get "error: no match for call to '(QTime) (int, int, int)'"

The only way that works, like Thomas1965 said, is to set the value (but not use the constructor):

time setHMS(0,1,0);

So I got it to work this last way, but am still wondering how I should call a constructor if I wanted to. Or it's just not possible unless I'm using a pointer? (QTime* time)
Thanks.
Last edited on
Line 5 is wrong. It is not calling the constructor. You are trying to call the operator() which most likely doesn't exists.
but am still wondering how I should call a constructor if I wanted to

I don't understand how you can still be wondering, when you've already been explicitly shown how to do it by Thomas1965.

You seem to have midunderstood how constructors work. The constructor is called when the object is created. It is used to set up the initial state of the object.

1
2
QTime t(1, 1, 1);  // This invokes the constructor that takes 3 ints
QTime tPtr = new QTime(1, 1, 1);  // This also invokes the same constructor 


You can't invoke the constructor after an object has been created, like you're trying to do at line 5 of your second code snippet.

1
2
QTime t;  // This invokes the default contructor
time(1, 1, 1);  // This DOES NOT work 


Clear now?

Last edited on
First, Qt is not your problem (it just helps to reveal the issue).
Your problem is that you don't know C++ classes properly.

1
2
3
4
5
6
7
8
9
MainWindow::MainWindow( QWidget* parent )
 :
  QMainWindow( parent ),
  ui( new Ui::MainWindow ), // #A
  timer( new QTimer ),  // #B
  time( 0, 1, 0 )
{
    ui->setupUi( this );
}

The bold part is member initializer list. It is part of class syntax.
The order of members in the list should match their order in the definition of the class.


Qt-related question:
#A and #B: dynamic allocation of objects. Who owns them? Who sees that they are properly destructed?

The Qt uic might generate code to manage #A, but it won't for #B. QObjects manage their children, but we did not use that. Should we?


Qt offers a lot of functionality but assumes that the user knows C++, just like AH-64 Apache can do a lot, if you know how to fly a helicopter.
Topic archived. No new replies allowed.