1

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

1 Answer 1

7

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

A::values A::stringToEnum (string inputString) {
//...
Sign up to request clarification or add additional context in comments.

2 Comments

thanks! every "new type member" inside the class, is supposed to be used by className::newType?
Inside the definition you can just use values and val1 etc. If you want to be forced to type values::val1 change values to a C++11 enum class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.