I have the following code in C++ and I got the compilation error:
a.cpp: In member function `virtual void Derived<T, D>::run(T&)':
a.cpp:13: error: expected primary-expression before "int"
a.cpp:13: error: expected `;' before "int"
Please help me find out what went wrong here. Thanks a lot.
#include <iostream>
template<typename T> struct Base
{
virtual void run( T& ){}
virtual ~Base(){}
};
template<typename T, typename D> struct Derived : public Base<T>
{
virtual void run( T& t )
{
D d;
d.operator()<int>();//nor does d.operator()<T>(); work
}
};
template<typename T> struct X
{
template<typename R> X(const R& r)
{
std::cout << "X(R)" << std::endl;
ptr = new Derived<T,R>();
}
X():ptr(0)
{
std::cout << "X()" << std::endl;
}
~X()
{
if(ptr)
{
ptr->run(data);
delete ptr;
}
else
{
std::cout << "no ptr" << std::endl;
}
}
Base<T>* ptr;
T data;
};
struct writer
{
template<typename T> void operator()()
{
std::cout << "T "<< std::endl;
}
};
int main()
{
{
writer w;
X<int> xi1((writer()));
}
return 0;
};
In
Derived<>::run(), changeto
For further information, see this FAQ:
What is the
->template,.templateand::templatesyntax about?