Active Topics

 


Reply
Thread Tools
Guest | Posts: n/a | Thanked: 0 times | Joined on
#1
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's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#2
enums must be defined in the header file - not the cpp.
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#3
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.
 

The Following User Says Thank You to Joorin For This Useful Post:
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#4
Originally Posted by nicolai View Post
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's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#5
Originally Posted by Joorin View Post
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.
 
Guest | Posts: n/a | Thanked: 0 times | Joined on
#6
Originally Posted by Joorin View Post
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.
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#7
Originally Posted by nicolai View Post
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

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.
 

The Following User Says Thank You to Joorin For This Useful Post:
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#8
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
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#9
Originally Posted by XenGi View Post
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.

Last edited by Joorin; 2011-04-04 at 10:45. Reason: Forgetfulness
 

The Following User Says Thank You to Joorin For This Useful Post:
Guest | Posts: n/a | Thanked: 0 times | Joined on
#10
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..

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();
 
Reply


 
Forum Jump


All times are GMT. The time now is 21:09.