简体   繁体   中英

CXX0030 Expression cannot be evaluated

I have a misunderstanding here. I'm designing a Queue class which uses the Client class as the base unit. The list structure consists of a pointer to the next data and a variable for holding the Client object.

The Queue class has 2 ways of operating. The way of it's operating is determined by the bool MODE variable. If mode equals 1, then the Queue class uses placement new operator, and if not, it uses the new operator.

This is the Queue class's prototype:

class Queue
{
private:
    const int max_lists;
    int no_lists, counter;
    char client_value;
    list *chunk; 

    list *top;
    const bool MODE;
    void addFirst(const Client &c);
    void addLast(const Client &c); 
    void deleteLast();
    void deleteFirst();

    void clean_mem();
    void clean_mem(list *&pos);
    list* malloc();//T* type
public:
    Queue();
    Queue(list *buffer,int no);
    Queue(const Queue &q);
    Queue& operator=(const Queue &q);
    ~Queue() { clean_mem(); }

    void enqueue(const Client &c);
    void timeoutCustomers();
    void decreaseTimeout();
    Client getCustomer() const;
    void finishCustomer();

    void show() const;
};

The functions definitions which contribute to the error given are here:

void Queue::addFirst(const Client &c)
{
    if(top==nullptr)
    {
        top = malloc();
        top->info = c;
        top->next = nullptr;
    }
    else
    {
        list *add = malloc();
        add->info = c;
        add->next = top;
        top = add;
    }
}


list* Queue::malloc()
{
    if(MODE)
    {
        if(no_lists==max_lists)
        {
            return nullptr;
        }
        else
        {
            list *tmp = chunk;
            counter = 0;
            while(counter++<max_lists)
            {
                 client_value = (char)tmp->info;
                 if(client_value==-1 && tmp->next==nullptr)
                 {
                     return new(tmp) list;
                 }
                 tmp++;
             }
             return nullptr;
         }

     }
    else
    {
         return new list;
    }
}

And here's the list structure:

struct list { Client info; list *next; };

When I make an instance of the Queue class, I can choose whether I go on placement new or just the new operator.

If I choose the placement new, I'm have to send the address of an array of type list. The address is saved into chunk pointer. The top pointer holds the first address of the linked list. Then, if I call the addFirst() function it stops at top->info = c. The error points to top->info :

 CXX0030: Error: Expression cannot be evaluated.

But when I switch back to new operator, everything works. This tells me that there's a problem with the allocation of a new portion in the already allocated memory.

Can somebody give me a direction of what's the problem here?

Did you intend your malloc function like that? You have an 'else' block AFTER your functions definition and not all control paths return a value. I have a rewrite here that seems to match more with what I think you actually intend:

list* Queue::malloc()
{
    if(MODE)
    {
        if(no_lists==max_lists)
        {
            return nullptr;
        }
        else
        {
            list *tmp = chunk;
            counter = 0;
            while(counter++<max_lists)
            {
                client_value = (char)tmp->info;
                if(client_value==-1 && tmp->next==nullptr)
                {
                    return new(tmp) list;
                }
                tmp++;
            }
            return nullptr;
        }
    }
    else
    {
        return new list;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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