maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   How to use enums (https://talk.maemo.org/showthread.php?t=71788)

XenGi 2011-04-04 09:57

How to use enums
 
Hi I made a class for notes and want to use an enum for a status like NEW or DELETED but I can't get it to work.
Heres the code:

note.h
Code:

#ifndef NOTE_H
#define NOTE_H

#include <QObject>

class Note : public QObject
{
    Q_OBJECT
public:
    Note(QString Text);
    Note(int Id, QString Text);
    enum Status;

private:
    int id;
    QString text;
    Status status;

};

#endif // NOTE_H

note.cpp
Code:

#include "note.h"

Note::Note(QString Text)
{
    id = NULL;
    text = Text;
    status = NEW;
}

Note::Note(int Id, QString Text)
{
    id = Id;
    text = Text;
    status = NORMAL;
}

enum Note::Status
{
    NORMAL = 0,
    NEW = 1,
    DELETED = 2
};

The resulting error message is "use of enum 'Status' without previous declaration". The error accours at the line " enum Status;" in note.h.

Can anyone tell me how to write this correctly?

nicolai 2011-04-04 10:09

Re: How to use enums
 
enums must be defined in the header file - not the cpp.

Joorin 2011-04-04 10:11

Re: How to use enums
 
The compiler is telling you what's wrong. The compiler is your friend.

"without previous declaration" indicates that you need to move the declaration of your enum to a place where it has been seen before you use it.

I'd suggest after the include and before the class declaration.

The compiler is your friend.

Joorin 2011-04-04 10:12

Re: How to use enums
 
Quote:

Originally Posted by nicolai (Post 981538)
enums must be defined in the header file - not the cpp.

No. Symbols need to be declared before they are used. If this enum was to be internal, it might very well be declared in the cpp file.

nicolai 2011-04-04 10:24

Re: How to use enums
 
Quote:

Originally Posted by Joorin (Post 981540)
No. Symbols need to be declared before they are used. If this enum was to be internal, it might very well be declared in the cpp file.

Yes you are right, but his:
Code:

public:
enum Status

looks like he doesn't want to use it internally only.

XenGi 2011-04-04 10:27

Re: How to use enums
 
Quote:

Originally Posted by Joorin (Post 981539)
I'd suggest after the include and before the class declaration.

So, I put that code between the include and class in the header file:

Code:

enum Status
{
    NORMAL = 0,
    NEW = 1,
    DELETED = 2
};

But I get the same error: "use of enum 'Status' without previous declaration".

And yes, I wanna use this enum also outside the class. ;)

Joorin 2011-04-04 10:29

Re: How to use enums
 
Quote:

Originally Posted by nicolai (Post 981547)
Yes you are right, but his:
Code:

public:
enum Status

looks like he doesn't want to use it internally only.

In this case it's part of the API but you wrote

Quote:

enums must be defined in the header file - not the cpp.
which is what commented on.

So, I think that we agree on how it should be done, you just got it a little bit too general in your answer.

nicolai 2011-04-04 10:41

Re: How to use enums
 
Code:

#ifndef NOTE_H
#define NOTE_H

#include <QObject>

class Note : public QObject
{
    Q_OBJECT
public:
    Note(QString Text);
    Note(int Id, QString Text);


enum Status
{
    NORMAL = 0,
    NEW = 1,
    DELETED = 2
};
private:
    int id;
    QString text;
    Status status;

};

#endif // NOTE_H


Joorin 2011-04-04 10:43

Re: How to use enums
 
Quote:

Originally Posted by XenGi (Post 981549)
So, I put that code between the include and class in the header file:

Code:

enum Status
{
    NORMAL = 0,
    NEW = 1,
    DELETED = 2
};

But I get the same error: "use of enum 'Status' without previous declaration".

And yes, I wanna use this enum also outside the class. ;)

So, I stripped away the Qt stuff and got this that compiles just fine:
Code:

enum Status
{
    NORMAL = 0,
    NEW = 1,
    DELETED = 2
};

class Note
{
public:
    Note();
    Note(int Id);

private:
    int id;
    Status status;

};

Note::Note()
{
    id = 0;
    status = NEW;
}

Note::Note(int Id)
{
    id = Id;
    status = NORMAL;
}

int main(int argc, char** argv) {
  Note foo;

  return 0;
}

Now look for differences apart from the Qt stuff.

Hint: Your enum is a symbol that's not part of the class. An enum is a place holder for integer values and nothing more. If you want to limit its visibility, use namespaces or declare it inside your class.

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.

Happy hacking.

XenGi 2011-04-04 11:55

Re: How to use enums
 
I'm now using the last version that nicolai has posted and everything works just fine. Thanks for your help. :)

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

Maybe someone could post a little summary about the meaning of int i, int *i and int &i. And why my compiler forbids me to do the following:

QDateTime *time;
time = QDateTime::currentDateTime();


All times are GMT. The time now is 00:27.

vBulletin® Version 3.8.8