Reputation: 419
I am learning C++ and am trying to create a function with a return type of "my_message_t" that was defined in the class definition. However when I tried to compile it, compiler informed me that error: ‘my_message_t’ does not name a type
I have the following code (protocols.h and protocols.cpp)
namespace Protocols {
class Messages {
public:
typedef struct _my_message_t {
//stuffs
} my_message_t;
typedef enum {
//stuffs
} my_message_e;
private:
my_message_t my_msg;
my_message_e msg_en;
public:
Messages();
~Messages();
my_message_t create_message(const my_message__e);
};
};
with the class definition is below:
namespace Protocols {
Messages::Messages(){
//stuff
};
Messages::~Messages(){
//stuffs
}
my_message_t Messages::create_message(my_message_e type){
my_message_t msg;
//do stuffs
return msg;
}
}
Why I can not create a function with a type of my_message_t
? How do I fix that code above?
Upvotes: 0
Views: 154
Reputation: 212959
Change:
my_message_t Messages::create_message(my_message_type_e type)
to:
Messages::my_message_t Messages::create_message(my_message_e type)
^^^^^^^^^^
Upvotes: 4
Reputation: 254451
my_message_t
is scoped inside the Messages
class, so it needs to be qualified when used outside a member of the class:
Messages::my_message_t Messages::create_message(my_message_e type){
^^^^^^^^^^
// do stuffs
}
Note that you don't need that in the function parameter list, only the return type; a quirk of the language means that the parameter list is scoped inside the function, and the return type outside.
Upvotes: 9
Reputation: 227390
my_message_t
is a type defined inside of class Messages
, so you need:
Messages::my_message_t Messages::create_message(my_message_type_e type)
Upvotes: 2