View Single Post
Posts: 560 | Thanked: 422 times | Joined on Mar 2011
#13
Pointers and references are great because the allow information to persist beyond its original scope. The best one can hope for, from value-data is to return a single object at the end a function.

Regarding pointers: always intialise them! If they're member variables, use an initialisation list on your constructor. Not least because you can test for an object, even if it's value is set to NULL (0). Howwever, you can't test an unset pointer. Uninitialised pointers are a massive invisible headache! Also, a pointer does not store data, it points to the place where it is stored. If the data does not exist, use 'new' to create a suitable container. Pointers are type-sensitive, unless you're using void * for function pointers, say.

Not having seen the code in motion, my guess is that you have
- created the pointer
- pointed it at a value, not the location of an object.
I also suspect that both QDateTime is non static i.e. an object definition rather than an object itself so another reason for the error. If it is static, you'll still get an error b/c of what the pointer's being asked to point at.

You need to
- create the pointer object (e.g. *pTime),
- point it at reference object, which may need creating/initialising.
- use the pointer. (to call a function say)

For example, to resolve this case you might create a (new) QDateTime obectj then call the currentTime() function whenever you need its output:

// create ptr and ref obj, which self inits
QDateTime *pTime = new QDateTime();

// get time
<Type> timeNow = pTime->currentTime();


- where <Type> is the correct type that the f'n returns.

Pointers and Refs just need practice. Try to learn what the compile errors mean in human, not compiler speak.

Good luck - look forward to seeing the software in devel!

ps Speaking from experience, match your new and delete operators carefully and be equally mindful about using const with pointers across scopes!

Last edited by demolition; 2011-04-04 at 15:46.
 

The Following User Says Thank You to demolition For This Useful Post: