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.
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.
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):
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.
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
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.