"." 不能出现在常量表达式中

'.' cannot appear in a constant-expression

本文关键字:表达式 常量 不能      更新时间:2023-10-16
template <class T>
struct stkNode
{
    BinTreeNode<T> *ptr;
    enum tag {R,L}tag;
    stkNode(BinTreeNode<T> *N = NULL) : ptr(N),tag(L){}
};
template<class T>
void BinaryTree<T>::PostOrder(void(*visit)(BinTreeNode<T> *p))
{
    SeqStack<stkNode<T> > S;
    stkNode<T> w;
    BinTreeNode<T> *p = root;
    do
    {
        while (p != NULL)
        {
            w.ptr = p;
            w.tag = w.L;
            S.Push(w);
            p = p->leftChild;
        }
        bool continuel = true;
        while (!S.IsEmpty() && continuel)
        {
            S.Pop(w); 
            p = w.ptr;
            switch (w.tag)
            {
            case w.L: //---------------this line--------------------------
                w.tag = w.R;
                S.Push(w);
                continuel = false;
                p = p->rightChild;
                break;
            case w.R: // -----------and this line-------------
                visit(p);
                break;
            }
        }
    } while (!S.IsEmpty());
}

当我在 Devc++ 上编译它时,它将是一个错误,如下所示:[错误]'." 不能出现在常量表达式中。但是当我在Visual Studio 2015上编译它时,错误不会发生。为什么??????

-----------更新我的问题--------------------如

    #include <iostream>
using namespace std;
struct exp
{
    char ch;
    enum dir{
        L,R
    }direction;
    exp(char name,dir d){
        ch = name;
        direction = d;
    }
};
int main()
{
    exp t('a',exp.L); //this line
    return 0;
}

是一样的

问题是我错误地访问枚举的方法.... 正确的代码是:

template<class T>
void BinaryTree<T>::PostOrder(void(*visit)(BinTreeNode<T> *p))
{
    SeqStack<stkNode<T> > S;
    stkNode<T> w;
    BinTreeNode<T> *p = root;
    do
    {
        while (p != NULL)
        {
            w.ptr = p;
            w.tag = w.L;
            S.Push(w);
            p = p->leftChild;
        }
        bool continuel = true;
        while (!S.IsEmpty() && continuel)
        {
            S.Pop(w); 
            p = w.ptr;
            switch (w.Tag)
            {
            case stkNode<T>::L:
                w.tag = w.R;
                S.Push(w);
                continuel = false;
                p = p->rightChild;
                break;
            case stkNode<T>::R:
                visit(p);
                break;
            }
        }
    } while (!S.IsEmpty());
}