Как исправить ошибку: «выражение нельзя использовать как функцию», когда я изменяю тип переменной с Private на Publi c? - PullRequest
0 голосов
/ 01 марта 2020

Попытка выполнить простую операцию постановки / снятия с очереди путем генерации двоичных чисел, но столкнулась с ошибкой в ​​ строка # 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 с private на publi c (Он выдавал error , говоря front is private , прежде чем я сделал его publi c)

.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];
    }
}

что я сделал не так?

1 Ответ

0 голосов
/ 01 марта 2020

В вашем классе Quetype front является переменной-членом, а не функцией-членом, вам нужно remove ( ).

Просто написать

string s1= q.front;
...