`". cannot appear in constant expression

I am getting the following error:

`.' cannot appear in a constant-expression

      

for this function (line 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?

EDIT:

scope definition:

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

      

when Range

has variables min

and max

.

+2


a source to share


4 answers


Trying your code tells me the compiler has a problem with the part me[d].max < other[d].max

. So the dot problem was bogus. Instead, the compiler has a problem with the comparison operator. Simply repeating the comparison made the compiler error magically disappear:



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

      

+2


a source


If stakx's answer isn't enough, you can look at the "min" and "max" variables. There might be some preprocessor definition preventing all of this from working.

Try adding



#undef min   
#undef max  

      

right before your code to see if the error is worth it.

+3


a source


My guess is that this fails because the operator is []

not a valid operation on your variables me

, other

etc.

  • Have you overloaded the operator[]

    to your class Region<>

    ? If so, does it return an object that actually has these members min

    and max

    ? - Does the overloaded operator return an object, an object by reference, or a pointer to an object? (In the latter case, you need to replace .

    with ->

    .)

  • If you haven't overloaded []

    , then me

    , other

    etc. would have to be declared as an array for your code to be valid.

+2


a source


This probably doesn't work because you haven't defined the [] (unsigned) const operator. I would also suggest using std::size_t

or int

as your loop variable; very rarely seen unsigned

. However, since you are using an unsigned type, the logical choice would be to use std::size_t

. You can also try calling this-> operator [] (d) instead of me [d] as a sanity check, although what should work for you should be fine if your class implements the appropriate operator overloading.

0


a source







All Articles