I am trying to call a member function of an object in a class template, but I cannot get the following code to compile. I found a post here that said I can use object.template method<T>();
.
Using MSVC 2015, I get error C2059: syntax error: 'template'
#include <iostream>
class Bar
{
public:
Bar() : m_x(0.f) { }
~Bar() { }
void setX(double x) { m_x = x; }
void printX(void) { std::cout << m_x << std::endl; }
private:
double m_x;
};
template <class T>
class Foo
{
public:
Foo() { }
~Foo() { }
void setBar(T bar) { m_bar = bar; }
void printBar(void) { m_bar.template printX<T>(); } // This is the issue
private:
T m_bar;
};
int main()
{
Bar bar;
bar.setX(20.f);
bar.printX();
Foo<Bar> foobar;
foobar.setBar(bar);
foobar.printBar();
return 0;
}