0

尝试通过生成二进制数来执行简单的入队/出队操作,但在第 18 行 Main中遇到错误:

    #include <iostream>
#include "quetype.cpp"
#include "quetype.h"
#include <queue>

using namespace std;

int main()
{

    QueType<string> q;
    int n = 10;

    q.Enqueue("1");

    while (n--)
    {
        string s1 = q.front(); //expression cant be used as a function
        q.Dequeue();
        cout << s1 << "\n";
        string s2 = s1;  
        q.Enqueue(s1.append("0"));
        q.Dequeue(s2.append("1"));

    }

}

这发生我将变量front的类型从私有更改为公共之后(在我将其公开之前,它给出了一个错误,说front是私有的)

.h 文件:

#ifndef QUETYPE_H_INCLUDED
#define QUETYPE_H_INCLUDED

class FullQueue
{};
class EmptyQueue
{};
template<class ItemType>
class QueType
{
    public:
        QueType();
        QueType(int max);
        ~QueType();
        void MakeEmpty();
        bool IsEmpty();
        bool IsFull();
        void Enqueue(ItemType);
        void Dequeue(ItemType&);
        int front;
        //int rear;
        //ItemType* items;
        //int maxQueue;
    private:
        //int front;
        int rear;
        ItemType* items;
        int maxQueue;
};

#endif // QUETYPE_H_INCLUDED

源cpp:

#include"quetype.h"
#include <queue>
template<class ItemType>
QueType<ItemType>::QueType(int max){
    maxQueue=max+1;
    rear=max;
    front=max;
    items= new ItemType[ maxQueue];
}
template<class ItemType>
QueType<ItemType>::QueType(){
    maxQueue=501;
    rear=maxQueue-1;
    front=maxQueue-1;
    items= new ItemType[ maxQueue];
}
template<class ItemType>
QueType<ItemType>::~QueType(){
    delete[] items;
}
template<class ItemType>
void QueType<ItemType>::MakeEmpty(){
    rear=maxQueue-1;
    front=maxQueue-1;
}
template<class ItemType>
bool QueType<ItemType>::IsEmpty(){
    return (rear==front);
}
template<class ItemType>
bool QueType<ItemType>::IsFull(){
    return ((rear+1)%maxQueue==front);
}
template<class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem){
    if(IsFull())
        throw FullQueue();
    else{
        rear=(rear+1)%maxQueue;
        items[rear]=newItem;
    }
}
template<class ItemType>
void QueType<ItemType>::Dequeue(ItemType& Item){
    if(IsEmpty())
        throw EmptyQueue();
    else{
        front=(front+1)%maxQueue;
        Item=items[front];
    }
}

我做错了什么?

4

1 回答 1

0

在您的 Quetype 类 front中是成员变量而不是成员函数,您必须remove ( ).

写吧

string s1= q.front;
于 2020-03-01T18:26:15.663 回答