理解常量表达式错误
understand a constant expression error
我很想了解以下错误的来源:
'。'不能出现在常量表达式中
多亏了这个链接,我已经知道如何解决这个问题了:"。"和恒定表达
事实上,我遇到了与上面链接中描述的问题相同的问题。我只是想了解为什么恢复比较会起作用。谢谢这是我的代码:
template <typename Key> std::vector<Segment<Key> > findIntersections(const Interval<Key> &interval ,Segment<Key> segment)
{
Interval<Key> *x = &interval;
vector<Segment<Key> > intersections;
while( x!= NULL)
{
if(x->intersects(segment.gety1(),segment.gety2())) intersections.push_back(segment);
else if (x->left == NULL) x = x->right;
//else if (x->left->max < segment.gety1()) x = x->right;//this line gives the error
else if (segment.gety1() > x->left->max) x = x->right;//this line is OK
else x = x->left;
}
return intersections;
}
编辑1
我在下面提供了更多的代码,伴随着上面的代码。
Interval<Key>
定义一个几何间隔(不要注意注释):
template <class Key> class Interval
{
public:
Key low;
//Key high;//previous implementation
std::vector<Key> high;//to implement... may be better to use a max priority queue
//A priority queue is better since accessing a value is in o(log N) - vector is in o(N)
//as well deleting and inserting is in o(log N) for priority queue
//a binary tree is good as well.
Key max;//to implement
Key back;//represent the maximum of the list of high
bool color;
int N; //number of nodes under this subtree
Interval *left, *right;
Interval(Key lo, Key hi, Key val, bool c): low(lo),max(val),back(val),color(c),N(1),left(NULL),right(NULL)
{
high.push_back(hi);
}
bool intersects(Key lo, Key hi);
};
分段类别:
template <class Type> class Segment
{
private:
Type x1, y1, x2, y2;
public:
Segment(Type x1, Type y1, Type x2, Type y2):x1(x1), y1(y1), x2(x2), y2(y2){}
inline bool isHorizontal(){return x1 == x2;}
inline bool isVertical (){return y1 == y2;}
int compare(Segment segment);
inline Type getx1(){return x1;}
inline Type getx2(){return x2;}
inline Type gety1(){return y1;}
inline Type gety2(){return y2;}
};
最后是相交法:
template <class Key> bool Interval<Key>::intersects(Key lo, Key hi)
{
if(lo <= this->low && this->back <= hi) return true;
if(this->low <= lo && hi <= this->back) return true;
if(lo <= this->low && hi <= this->back) return true;
if(this->low <= lo && this->back <= hi) return true;
return false;
}
如果你需要更多,请告诉我。。。谢谢大家的帮助。。。
编辑2
我使用的是适用于windows的gcc 4.6.1编译器:MinGW-32位
您的代码中有using namespace std;
吗?这是唯一的办法,我可以重现你的错误。请参见此处。看起来,编译器将max
视为模板函数,因此将<
视为参数列表的开头。
PS:无论我的代码中是否有using namespace std
,您的代码在MSVC 2013 Express中运行良好。
此外,这个链接可能会解释更多关于你的问题。
编译器似乎以某种方式(宏开关似乎最有可能)认为max
标识符后面的<
符号不是比较运算符,而是一个打开的模板参数列表括号。因此,后面的内容必须是类型名或常量表达式。
相关文章:
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 错误:表达式必须是可修改的 I值
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 错误:表达式不能用作函数
- C++/错误:表达式必须具有整数或无作用域枚举类型
- 错误表达式必须是插入函数(二叉树 C++)中的可修改 Ivalue
- 合并排序错误 - 表达式必须具有常量值
- 数组声明中的错误:表达式必须具有常量值
- 错误:表达式列表在初始化器[-fpermissive] Double Paycalc(p,yir,y)中被视为复合表达式
- 错误:表达式不能用作函数
- 我该如何解决此错误:表达式必须具有恒定值.环境是2013年
- 错误:表达式必须是可修改的LVALUE
- 错误:表达式必须具有指向类C++的指针
- 错误:表达式不能用作函数
- 错误:表达式必须具有积分或未引人注目的枚举类型
- 参数化sc_lv时报告错误(表达式必须具有常量值)
- 无法使用函数变量?错误表达式必须具有类类型
- 错误:表达式必须具有类类型
- 错误:表达式不能用作函数.如何删除它
- C++ - 错误:表达式必须具有指向对象的指针类型