在CODBROCKC++中发现的在“>”令牌之前的意外主表达式
以下代码可以在VC10中很好地编译:在CODBROCKC++中发现的在“>”令牌之前的意外主表达式,c++,linux,codeblocks,C++,Linux,Codeblocks,以下代码可以在VC10中很好地编译: #include <vector> #include <iostream> #include <math.h> #define _USE_MATH_DEFINES #include <math.h> #include <assert.h> #include <iostream> #include <vector> #include <utility> #in
#include <vector>
#include <iostream>
#include <math.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <assert.h>
#include <iostream>
#include <vector>
#include <utility>
#include <functional>
#include <numeric>
#include <algorithm>
#include <limits>
template <typename T>
class Coordinate
{
public:
T x_; ///< x_coordinate
T y_; ///< y_coordinate
/**
* Default constructor
*/
Coordinate():x_(0),y_(0){};
/**
* Default deconstructor
*/
~Coordinate() {};
/**
* distance between two points
*/
template<typename outputType>
outputType distance( const Coordinate<T> &obj) const
{
outputType dis_x = x_-obj.x_;
outputType dis_y = y_-obj.y_;
return static_cast<outputType>(sqrt( (double)(dis_x*dis_x)+(double)(dis_y*dis_y)));
}
};
/**
* This function is used to calculate the distance between two points
* @relates Coordinate
* @param pt1 Point 1
* @param pt2 Point 2
* @return the distance
*/
template<typename BaseType, typename OutputType >
inline OutputType Distance(const Coordinate<BaseType> &pt1, const Coordinate<BaseType> &pt2)
{
OutputType dis;
dis = pt2.distance<OutputType>(pt1);
return dis;
}
/**
* This function is used to calculate the distance between one point with
* an array of data sets.
*/
template<typename BaseType, typename OutputType>
void Distance(const std::vector<Coordinate<BaseType> > &pt_array,
const Coordinate<BaseType> &pt,
std::vector<OutputType> &dis_array)
{
int len = pt_array.size();
dis_array.resize(len);
for(int i=0; i<len; i++)
{
dis_array[i] = pt.distance<OutputType>(pt_array[i]);
}
}
int main()
{
int a;
Coordinate<float> pt1;
Coordinate<float> pt2;
pt1.x_ = 2;
pt1.y_ = 3;
pt2.x_ = 4;
pt2.y_ = 5;
a = Distance<float,int>(pt1,pt2);
std::cout<<a<<std::endl;
return 1;
}
但是,当使用g++4.6在ubuntu linux代码块中编译时,会出现以下错误:
In function ‘OutputType Distance(const Coordinate<BaseType>&, const Coordinate<BaseType>&)’:
error: expected primary-expression before ‘>’ token
In function ‘void Distance(const std::vector<Coordinate<BaseType> >&, const Coordinate<BaseType>&, std::vector<OutputType>&)’:
error: expected primary-expression before ‘>’ token
有什么想法吗?非常感谢 由于pt2属于模板类型,因此必须指定调用的函数为模板:
dis = pt2.template distance<OutputType>(pt1);
Visual Studio可以在不使用template关键字的情况下解决此问题,但这不是标准行为。由于pt2是模板类型,因此必须指定调用的函数是模板:
dis = pt2.template distance<OutputType>(pt1);
Visual Studio可以在不使用template关键字的情况下解决此问题,但这不是标准行为。非常感谢,它可以正常工作。但是为什么在VC10中不需要指定模板呢?如果你能给我提供消息来源那就太好了。谢谢@YelFube:微软C++在模板被定义时不执行两级名称查找,并且在C++标准指定的实现时执行一次。相反,它延迟所有名称查找,直到模板被实例化。在这一点上,它可以判断从属名称是否引用了成员、类型或模板,而无需告知;因此,作为一个扩展,它允许您省略其他编译器所需的模板或类型名说明符。非常感谢,它可以工作。但是为什么在VC10中不需要指定模板呢?如果你能给我提供消息来源那就太好了。谢谢@YelFube:微软C++在模板被定义时不执行两级名称查找,并且在C++标准指定的实现时执行一次。相反,它延迟所有名称查找,直到模板被实例化。在这一点上,它可以判断从属名称是否引用了成员、类型或模板,而无需告知;因此,作为扩展,它允许您省略其他编译器所需的模板或类型名说明符