How to solve the Template compile error

How to solve the Template compile error

Hello everyone,

I have a error when i compile my code which using c++ template, the code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <map>
#include <string>
#include <memory>

template<class T >
struct ExpressionTrait ;


class IA
{
public:
        virtual void func() = 0;

};

class A : public IA
{
public:
       
        void func()
       {
              std::cout << "sdfdsf" << std::endl;
       }
};

template<>
struct ExpressionTrait <A >
{
        typedef IA parentType;
        static const int max = 100;
        static const int min = 1;
};


template<typename ExpressionObject >
class ExpressionFactory
{
public:
        static ExpressionFactory<ExpressionObject >& Instance()
       {
               static ExpressionFactory<ExpressionObject > instance;
               return instance;
       }

        typedef std:: map< int, int> RegisterCreatorMap;


        template< typename T>
        void registerCreator()
       {
              std::cout << "sdfdsf" << std::endl;

              creatorMap_.insert(std::make_pair( ExpressionTrait<T >::max,
                       ExpressionTrait<T >::min));
       }

        int createExpression( int type)
       {
               typename RegisterCreatorMap::iterator typeIter = creatorMap_.find(type);
               if (typeIter != creatorMap_.end())
              {
                       int creator = typeIter ->second;
                       return creator;
              }

               return -1;
       }



        RegisterCreatorMap creatorMap_;
};

template<class T >
struct ExpressionFactoryAutoRegister
{
       ExpressionFactoryAutoRegister()
       {
          // This line cannot be compiled  
          ExpressionFactory<typename ExpressionTrait<T >::parentType >::Instance().registerCreator<T>();
       }
};


int _tmain (int argc , _TCHAR * argv [])
{
        ExpressionFactoryAutoRegister<A> temp;
        //ExpressionFactory<IA>::Instance().registerCreator<A>();
        int a = ExpressionFactory<IA >::Instance().createExpression(100);
       getchar();
        return 0;
}
  



I can compile this code in VS2015, but cannot compile by GCC, the error message is :
ExpressionFactory.hpp:156:107: error: expected primary-expression before '>' token
ExpressionFactoryT<typename ExpressionTrait<T >::parentType >::Instance().registerCreator<T>();
^
/containers/lewguo/lewguo/rootfs/home/lewguo/s/scfupgrade/src/rules/DependenceModParamConditionalExpressionRelative/ExpressionFactory.hpp:156:109: error: expected primary-expression before ')' token
ExpressionFactoryT<typename ExpressionTrait<T >::parentType >::Instance().registerCreator<T>();


Could you help me please? Thanks:)
Last edited on
1
2
3
// ExpressionFactory<typename ExpressionTrait<T >::parentType >::Instance().registerCreator<typename T>();

ExpressionFactory< typename ExpressionTrait<T>::parentType >::Instance().template registerCreator< /*typename*/ T >();


And to link, the static members of ExpressionTrait<A> should be defined somewhere:
1
2
const int  ExpressionTrait<A>::max ;
const int  ExpressionTrait<A>::min ;
Thanks, it works! :)

and 'static const' intger value can be initialized like

static const int max = 100;

Thanks you again:)
Topic archived. No new replies allowed.