user1673206
user1673206

Reputation: 1711

compilation error- enum within a class

Im having a compilation error and i cant figure why. I have enum declaration on the .h file and the .cpp file suppose to use it inside stringToEnum() function

this is the .cpp file

#include "A.h"

A::A(void)
{
}

A::~A(void)
{
}       

values A::stringToEnum (string inputString) {
    if (inputString == "string1") return val1;
    if (inputString == "string2") return val2;
}

this is the header file

class A
{
public:

    A(void);
    ~A(void);


private:
    enum values{
        val1,
        val2,
        val3,
        val4
    };
    values stringToEnum (string inputString);
};

this is the error I get:

1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2143: syntax error : missing ';' before 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2556: 'int A::stringToEnum(std::string)' : overloaded function differs only by return type from 'A::values A::stringToEnum(std::string)'
1>          c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'
1>c:\users\documents\visual studio 2010\projects\A.cpp(25): error C2371: 'A::stringToEnum' : redefinition; different basic types
1>          c:\users\documents\visual studio 2010\projects\A.h(22) : see declaration of 'A::stringToEnum'

I will be happy for some guidance.

thanks

Upvotes: 1

Views: 391

Answers (1)

TartanLlama
TartanLlama

Reputation: 65620

Since values is contained in A, you need to qualify the name:

A::values A::stringToEnum (string inputString) {
//...

Upvotes: 7

Related Questions