我举了以下例子来说明我的问题:
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);
}
上述代码可以使用Visual Studio 2010在Window中编译,但不能在linux中使用gcc4编译。*。在linux中,它会出现以下编译错误:
for obj[i].fun<int>(abc);, expected ";"before "int"
有什么想法吗?
答案 0 :(得分:1)
在课程声明后你缺少一个分号,并且在方法之后你不需要分号:
class A
{
public:
template<typename T>
void fun(T &abc)
{
}
};
如果您修复了上述问题并且编译器仍然在解析函数时遇到问题,请尝试以下语法:
obj[i].template fun<int>(abc);