Active Topics

 


Reply
Thread Tools
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#11
Code:
Note::Note(QString Text)
{
    id = NULL;
    text = Text;
    status = NEW;
}
Just initialize your id variable with 0 instead of NULL.
NULL is used in C-code to initialize pointer variables with
a NULL-pointer. (In c++-code you can just use 0 for NULL-Pointer.)

Originally Posted by XenGi View Post
Maybe someone could post a little summary about the meaning of int i, int *i and int &i.
http://www.infernodevelopment.com/si...and-references
http://www.parashift.com/c++-faq-lite/index.html
Originally Posted by XenGi View Post
And why my compiler forbids me to do the following:

QDateTime *time;
time = QDateTime::currentDateTime();
Follow the advice from Joorin:
Originally Posted by Joorin View Post
Hint 2: If you want to get help with compiling things, paste all of what the compiler is complaining about together with the offending part of code. In this case, a line number would have been nice.
 

The Following 2 Users Say Thank You to nicolai For This Useful Post:
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#12
Originally Posted by XenGi View Post
The only problems I have now is my limited knowlage about * and & symbols before variables. so I have to deal with several "can not convert int* to int" errors..

Maybe someone could post a little summary about the meaning of int i, int *i and int &i.
int i;
TYPE INT
Can contain a signed integer (size depending on platform)

int *i;
POINTER TO TYPE INT
Can contain a (memory) pointer to TYPE INT. Pointers are typically implementation dependant but you can in most cases think of them as TYPE UNSIGNED INT. This means that you can do arithmetic with them (POINTER + POINTER, POINTER++ , ...).

int &i;
REFERENCE TO TYPE INT
Think of this as an alias for your variable. It is, in all interesting ways, the same thing as what it is referencing. References can never be unassigned. Entering a scope with an unassigned reference will generate a compilation error.

Examples:
Code:
int i;
i = 17;

int *pInteger = &i;
The i variable now is a container for the signed integer 17.

pInteger is a POINTER TO TYPE INT that points at the place in memory where this 17 is stored. The "&" operator returns POINTER TO TYPE <TYPE>. This is not the same as REFERENCE TO TYPE <TYPE>. Pointers can be unassigned or NULL or pointing at whatever you like, it's up to you to make sure it has a value that makes sense.

Code:
int &rInteger = i;
rInteger is now a REFERENCE TO TYPE INT and any time that you manipulate its value, you will at the same time change the value in the variable i.

This mostly boils down to CALLING BY VALUE or CALLING BY REFERENCE. By using references as arguments when you call a method, no copy of the arguments needs to be done. C++ offers this as a way to not having to use pointers all over the place but puts strict rules on the usage of references.

Code:
int j = *pInteger;
The "*" operator dereferences the pointer, as in, it follows the pointer and returns whatever it's pointing at.

j will now contain whatever pInteger is pointing at, which in this case will be 17.

And why my compiler forbids me to do the following:

QDateTime *time;
time = QDateTime::currentDateTime();
Your time variable is of type POINTER TO TYPE QDateTime and I'm guessing that the method you're calling, currentDateTime(), returns an object and not a pointer to an object.

Look for the documentation for method and you'll know. The compiler most likely told you that it can't convert TYPE QDateTime to POINTER TO TYPE QDateTime. The compiler is your friend. Listen to it.

Last edited by Joorin; 2011-04-04 at 16:01. Reason: Typo
 

The Following 2 Users Say Thank You to Joorin For This Useful 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:
Guest | Posts: n/a | Thanked: 0 times | Joined on
#14
Thanks for the explanation. Many things are clearer now.
When the program is ready I will upload it to extras-devel.
If you are interested in the current online version of it you can test it here: http://xengi.ath.cx/webnotes
The maemo version will be able to syncronice the notes between the phone and web version.

The source can be accessed here: http://xengi.ath.cx/svn/webnotes

Last edited by XenGi; 2011-04-06 at 12:38. Reason: added vn repository
 

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


 
Forum Jump


All times are GMT. The time now is 07:13.