如何解决错误:将变量的类型从Private更改为Public时,“表达式不能用作函数?

问题描述 投票:0回答:1

试图通过生成二进制数来执行简单的入队/出队操作,但在line#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更改为public(在我将其设为public之前,它给出了error表示front is private

。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]; } }
我做错了什么?

[试图通过生成二进制数来执行简单的入队/出队操作,但在第18行主程序中遇到错误:#include

#include“ quetype.cpp” #include“ quetype.h” #include&...

c++ data-structures queue deque enqueue
1个回答
0
投票
在您的Quetype类中,

front

© www.soinside.com 2019 - 2024. All rights reserved.