-2

I give the following example to illustrate my question:

class A
{
   public:
      template<typename T>
      void fun(T &abc)
      {

       };

}
template<typename U>
void my_fun(std::vector<U> &obj)
{
  int abc;
  for(int i=0; i<obj.size(); i++
       obj[i].fun<int>(abc);

}

The above codes can be compiled in Window with Visual Studio 2010 but not in linux with gcc4.*. In linux, it gives the following compilation errors:

for        obj[i].fun<int>(abc);, expected ";"before "int"

Any ideas?

melpomene
  • 84,125
  • 8
  • 85
  • 148
feelfree
  • 11,175
  • 20
  • 96
  • 167
  • 1
    You might want `obj[i].template fun(abc);`. – songyuanyao Jan 27 '17 at 13:49
  • or even `class A {...};` – UKMonkey Jan 27 '17 at 13:50
  • `std::vector` is undeclared. – melpomene Jan 27 '17 at 13:51
  • @songyuanyao Thanks, and could you explain why? – feelfree Jan 27 '17 at 13:53
  • Msvc is not standard compliant about template with the required 2 passes. – Jarod42 Jan 27 '17 at 13:54
  • @feelfree, `std::vector` could have a specialization where `fun` isn't a function template. The compiler must assume `fun` is a data member when it can't know for sure, in which case `fun` is parsed as `fun < int > ...`, using less-than and greater-than operators. GCC's error probably comes from expecting `int` to start a declaration, in which case the declaration should be in a new statement. – chris Jan 27 '17 at 14:10

1 Answers1

1

You are missing a semicolon after the class declaration, and you don't need the semicolon after the method:

class A
{
   public:
      template<typename T>
      void fun(T &abc)
      {

      }
};

If you fix the above and the compiler still has issues resolving the function, try the following syntax:

obj[i].template fun<int>(abc);
Venemo
  • 18,515
  • 13
  • 84
  • 125