M.T M.T - 3 months ago 20
C++ Question

How to set the default value of enum type variable?

How to set the default value of the enum type tip, I tried setting it to 0 or 1, or nothing, but I get the same error?

enum tip {
pop,
rap,
rock
};

class Pesna{
private:
char *ime;
int vremetraenje;
tip tip1;

public:
//constructor
Pesna(char *i = "NULL", int min = 0, tip t){
ime = new char[strlen(i) + 1];
strcpy(ime, i);
vremetraenje = min;
tip1 = t;
}

};

Answer

You must set it to one of the enum values, like:

Pesna(char *i = "NULL", int min = 0, tip t = pop)
                                        // ^^^^^

Another techique is to use a Default value declared in the enum itself and use that one. This makes it easier if you change your mind later what the default should be:

enum tip {
    pop,
    rap,
    rock,
    Default = rap, // Take care not to use default, that's a keyword
};

Pesna(char *i = "NULL", int min = 0, tip t = Default)
                                        // ^^^^^^^^^
Comments