简体   繁体   English

`“。 不能出现在常量表达式中

[英]`.' cannot appear in a constant-expression

I'm getting the following error: 我收到以下错误:

`.' cannot appear in a constant-expression

for this function (line 4): 对于此功能(第4行):

    bool Covers(const Region<C,V,D>& other) const {
        const Region& me = *this;
        for (unsigned d = 0; d < D; d++) {
            if (me[d].min > other[d].min || me[d].max < other[d].max) {
                return false;
            }
        }

can anyone explain the problem please? 谁能解释这个问题?

EDIT: 编辑:

the definition of Region is: 区域的定义是:

template <typename C, typename V, unsigned D>
class Region : public boost::array<Detail::Range<C>,D>

when Range has a min and max variables. Range具有minmax变量时。

If stakx's answer is not sufficient, you may want to look into "min" and "max" variables. 如果stakx的答案还不够,您可能需要研究“ min”和“ max”变量。 There may be some preprocessor definition, preventing the whole thing from working. 可能存在一些预处理器定义,从而阻止了整个工作。

Try adding 尝试添加

#undef min   
#undef max  

just before your code, to see if the error stands. 在代码之前,查看错误是否仍然存在。

I assume this fails because the [] operator is not a valid operation on your variables me , other , etc. 我认为这失败了,因为[]运算符不是对变量meother等的有效操作。

  • Did you overload the [] operator on your Region<> class? 您是否在Region<>类上重载了[]运算符 If so, does it return an object which actually has these min and max members? 如果是这样,它是否返回实际上具有这些minmax成员的对象? — Does the overloaded operator return an object, an object by reference, or a pointer to an object? —重载的运算符是否返回对象,按引用对象或指向对象的指针? (In the last case, you'd need to replace . by -> .) (在后一种情况下,你需要更换.通过-> 。)

  • If you haven't overloaded [] , then me , other etc. would have to be declared as an array for your code to be valid. 如果您还没有重载[] ,那么必须将meother声明为数组,以使您的代码有效。

Trying out your code tells me, that the compiler has a problem with the me[d].max < other[d].max part. 试用您的代码会告诉我,编译器的me[d].max < other[d].max部分有问题。 So the problem with the dot was bogus. 因此,点的问题是虚假的。 Instead the compiler has a problem with the comparison operator. 相反,编译器的比较运算符有问题。 Just reverting the comparison made the compiler error magically disappear: 只需还原比较,即可使编译器错误神奇地消失:

if (me[i].min > other[i].min || other[i].max > me[i].max) {
       return false;
}

This is probably failing because you have not defined operator[](unsigned)const. 这可能是失败的,因为您尚未定义operator [](unsigned)const。 I would also suggest that you use std::size_t or int as your loop variable; 我还建议您使用std::size_tint作为循环变量; it is very uncommon to just see unsigned . 看到unsigned是非常罕见的。 Since you are using an unsigned type, though, the logical choice would be to use std::size_t . 但是,由于您使用的是无符号类型,因此逻辑选择是使用std::size_t You could also try invoking this->operator[](d) instead of me[d] just as a sanity-check, although what you have should work fine assuming that your class implements the appropriate operator overload. 您也可以尝试使用this-> operator [](d)而不是me [d]进行健全性检查,尽管假设您的类实现了适当的运算符重载,您所拥有的也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM