Structure does not name type in C ++

I get the problem and return type is structure

Example.h class Example { private: typedef struct connection_header { string url; string method; }; static connection_header get_connection_header(); }; Example.cpp connection_header Example::get_connection_header() { return NULL; } 

I get 'connection_header' does not name a type

May I find out why this error

+7
source share
3 answers

You are using typedef without specifying a name for this type. Just omit typedef , it is not needed here:

 struct connection_header { string url; string method; }; 

Next, connection_header declared inside the Example class, so you need to fully define its name in the implementation when it is a return type:

 Example::connection_header Example::get_connection_header() 
+8
source

Try entering the code in a cpp file, add Example:: to connection_header :

 Example::connection_header Example::get_connection_header() { return NULL; } 

connection_header defined inside Example , so you must specify its scope.

In addition, the typedef keyword will be ignored in C ++. You can omit it

+2
source

First, in C ++ (but not in C), every struct or class names a type. Therefore, if you declare a struct connection_header , you also get the type connection_header , so you can later declare connection_header var some variable.

Then a typedef both C and C ++ requires a type and a name. For example:

  typedef long my_number_type; 

declares my_number_type as a synonym for long

As others pointed out, omit typedef

+1
source

All Articles