"using typedef-name ... as a class" in a direct declaration

I am doing some policy-based projects here, and I need to type many types of templates to shorten the names.
Now the problem is that when I need to use a pointer to one of these types, I try to just forward-declare it, but the compiler complains about test.cpp:8: error: using typedef-name 'Test1' after 'class'
This is not about size, since I donโ€™t need the obj object at all, its just a pointer in the .h file, where I donโ€™t want to give the whole template.
This is g ++:

 //Works class Test{}; class Test; //Doesn't work class Test{}; typedef Test Test1; class Test1; 

Any clues?

+6
c ++ compiler-construction typedef forward-declaration
source share
3 answers

It is right. The typedef name cannot be used in such a direct declaration (this is technically called a specified type specifier, and if such a qualifier resolves the typedef name, the program is poorly formed).

I donโ€™t understand why you first need an advanced declaration. Because if you already have a typedef, why not just use this typedef? It also means this class.

Change From your comment, it seems that you need the assigned header of the header declaration, pretty much in the same sense <iosfwd> . So, if your template is called Test_Template_Name_Long , this header might look like

Testfwd.h

 template<typename A, typename B> class Test_Template_Name_Long; typedef Test_Template_Name_Long<int, bool> Test1; 

Then you can simply use this heading instead of making class Test1 , which the compiler has no idea what it is (and will think that it is a new class, regardless of any template and typedef).

+15
source share
 typedef Test Test1; class Test1; 

does not work because the typedef statement serves as a declaration for type Test1.

Basically, when the compiler sees a typedef, it remembers that Test1 is synonymous with Test. Then when he sees class Test1; , he thinks you are introducing a new type. But this cannot be regarded as an declaration, since the name Test1 is already used by typedef.

If you want to use the forward declaration for Test1, for example, you must put a typedef in each file, using that typedef as a direct declaration. Therefore, instead of class Test1; you should:

 class Test; typedef Test Test1; 
+2
source share

Another case when this situation arises:

 // File: network_fwd.hpp #ifndef __NETWORK_FWD_HPP__ #define __NETWORK_FWD_HPP__ class Network; typedef Network GarnetNetwork; #endif // __NETWORK_FWD_HPP__ // File: network.hpp #include "network_fwd.hpp" class GarnetIntLink : public BasicLink { public: friend class GarnetNetwork; }; 

I had to do this because there really was a GarnetNetwork class that I combined into a Network class and I wanted to distribute it throughout the rest of the code using this typedef. GCC still gives this message: error: using typedef-name 'GarnetNetwork after' class .

I really appreciate your help in this case.

NB: By the way, I posted this question here because I thought it was almost the same thing and not to distract the community.

+2
source share

All Articles