【问题标题】:cannot access private member declared in class: queue class template无法访问在类中声明的私有成员:队列类模板
【发布时间】:2013-10-26 13:35:43
【问题描述】:

我必须编写代码来实现模板队列 我收到此错误:无法访问在类中声明的私有成员 在这一行

    front=front->next;

这是我得到错误的代码的头文件:

#include <iostream>
#pragma once
using namespace std;
typedef int Error_code;
#define SUCCESS 0
#define OVERFLOW -1
#define UNDERFLOW -2

template <class T>
class Node{
T item;
Node * next;
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};

template <class T>
class queue
{
protected:

    Node<T>* front;                                             // pointer to front of Queue
    Node<T> * rear;                                             // pointer to rear of Queue
    int count;                                                  // current number of items in Queue



public:
    queue();                                        
    ~queue();
    bool isempty(){
    //return count == 0;
    if(front==NULL)
        return true;
    else 
        return false;
    };
    bool isfull(){return false;};
    Error_code serve(){
    Error_code outcome = SUCCESS;
    Node<T> *p;
    if(isempty()){
        cout<<"empty queue";
        outcome=UNDERFLOW;
    }
    else{
    p=front;
    front=front->next;
    delete p;
    count--;
    }
    return outcome;
    } ;
    Error_code retrieve(T &item){
    Error_code outcome SUCCESS;
    if(isempty())
    {           // front node is empty, queue is empty
        //return false;
        cout<<"empty queue";
        outcome=UNDERFLOW;
    }
    return outcome;
    };      


    Error_code append(T item){


    Node<T> * n ;
    n= new Node;                // create node
    n->item = item;                 // set node pointers
    n->next = NULL;     


    if (isempty())      
        {
            rear=front = n; 
        }
    else
    {
        rear->next = n;             // else place at rear
    rear = n;                           // have rear point to new node
    }
    count++;
    return SUCCESS;
    };                                          
};

【问题讨论】:

  • 你的程序中有三个语法错误:next=NULL:应该是next=NULL; -- Error_code outcome SUCCESS;应该是Error_code outcome = SUCCESS; -- n= new Node;应该是n= new Node&lt;T&gt;;
  • nextNode 类中是私有的。
  • 我更正了@DyP 注意到的语法错误,我将 next 更改为 public,但现在我收到此错误:错误 5 错误 LNK2019: unresolved external symbol "public: __thiscall queue ::~queue(void)" (??1?$queue@H@@QAE@XZ) 在函数_main中引用

标签: c++ templates


【解决方案1】:

front 是指向Node 的指针,但nextNode 中的私有成员

template <class T>
class Node{
T item;                         // since you didn't specify access level
Node * next;                    // explicitly the access is private by default
Node(){item=0; next=NULL;}
Node(T n){item=n; next=NULL:}
};

所以你不能在queue类中使用它:

front=front->next; // error

将其更改为public 或重新设计程序

【讨论】:

  • “将其更改为公开或重新设计程序”或成为朋友queue&lt;T&gt;
  • 我将其更改为 public,但现在我收到此错误:错误 5 错误 LNK2019:未解析的外部符号“public:__thiscall queue::~queue(void)”(? ?1?$queue@H@@QAE@XZ) 在函数_main中引用
  • @user2908749 您已在示例中声明,但未定义 queue 的 ctor 和 dtor。
  • @DyP 我在 .cpp 文件中这样做了
  • @DyP 我将 ctor 和 dtor 的定义移到了头文件中,现在可以使用了。
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 2014-08-23
相关资源
最近更新 更多