c++ - Why can't i use struct nested in a struct as a type to declare variable in a class template? -
this question has answer here:
you see, structs, put structs inside structs , try use these nested structs in class template declare variables. problem is: doesn't seem work expected. minimal example code:
#include "stdafx.h" #include <iostream> struct t1 { struct nested { int var1 = 12345; }; }; struct t2 { struct nested { float var1 = 67890; }; }; template <typename t > class proletarian { public: t * t; //works //t::nested * tn; ****** doesn't work! ******* proletarian<typename t>() { t::nested * tnested = new t::nested; //works std::cout << tnested->var1; } }; int _tmain(int argc, _tchar* argv[]) { proletarian<t1> t1 = proletarian<t1>(); proletarian<t2> t2 = proletarian<t2>(); return 0; }
i use visual studio 2013, intellisense ok code, won't compile these 2 errors:
[line 20 column 1] error c2143: syntax error : missing ';' before '*'
[line 20 column 1] error c4430: missing type specifier - int assumed. note: c++ not support default-int
i'm not @ c++, don't quite understand how templates work , why happens.
when compiler first passes on proletarian - before sees instantiation specific type t
- need know t::nested
refer type, can give using typename
follows:
template <typename t> class proletarian { public: typename t::nested* tn; proletarian<t>() { typename t::nested* tnested = new typename t::nested; std::cout << tnested->var1; } };
Comments
Post a Comment