2016-10-07 3 views
-2

私は、QueueDequeまたはStackDequeをユーザの選択に基づいてインスタンス化し、その中にウィジェットを格納しています。私はStackDeque/QueueDequeがメンバ関数を呼び出すたびになぜ私が '私的な'エラーになっているのか分かりません。それは私がそれらをどのようにインスタンス化しているかと関係がありますか?私はまだハンドラを学んでいない。クラスメンバーを使用して別のクラスのプライベートメンバーにアクセスするときに、「プライベート」エラーが発生するのはなぜですか?

DequeArray.h:

#if !defined NULL 
#define NULL 0 
#endif 

#if !defined (DEQUEARRAY_H) 
#define DEQUEARRAY_H 
#include <iostream> 

template < class T > 
class DequeArray 
{ 
    private: 
     int max_queue; 
     T** items; 
     int front; 
     int back; 
     int sz; 

     void arrayResize(int new_size); 

    public: 
     DequeArray(); 
     ~DequeArray(); 

     bool isEmpty(); 
     int size(); 
     void dequeueAll(); 

     T* peek(); 
     T* peekDeque(); 

     void enqueue(T* item); 
     void enqueueDeque(T* item); 

     T* dequeue(); 
     T* dequeueDeque(); 
}; 

template < class T > 
DequeArray<T>::DequeArray() 
{ 
    max_queue = 2; 
    items = new T*[max_queue]; 
    front = 0; 
    back = max_queue - 1; 
    sz = 0; 
} 

template < class T > 
DequeArray<T>::~DequeArray() 
{ 
    delete[] items; 
} 

template < class T > 
bool DequeArray<T>::isEmpty() 
{ 
    return sz == 0; 
} 

template < class T > 
int DequeArray<T>::size() 
{ 
    return sz; 
} 

template < class T > 
void DequeArray<T>::dequeueAll() 
{ 
    delete[] items; 

    max_queue = 2; 
    items = new T*[max_queue]; 
    front = 0; 
    back = max_queue - 1; 
    sz = 0; 
} 

template < class T > 
T* DequeArray<T>::peek() 
{ 
    T* item = NULL; 
    if (!isEmpty()) 
    { 
     item = items[front]; 
    } 
    return item; 
} 

template < class T > 
void DequeArray<T>::enqueue(T* item) 
{ 
    if (sz == max_queue) 
    { 
     arrayResize(2*max_queue); 
    } 

    //back = (back + 1) % (max_queue); 
    back = back + 1; 
    if (back == max_queue) back = 0; 
    items[back] = item; 
    sz++; 
} 

template < class T > 
T* DequeArray<T>::dequeue() 
{ 
    T* item = NULL; 

    if (!isEmpty()) 
    { 
     item = items[front]; 
     items[front] = NULL; 
     //front = (front + 1) % (max_queue); 
     front = front + 1; 
     if (front == max_queue) front = 0; 
     sz--; 
    } 
    return item; 
} 

template < class T > 
T* DequeArray<T>::peekDeque() 
{ 
    T* item = NULL; 
    if (!isEmpty()) 
    { 
     item = items[back]; 
    } 
    return item; 
} 

template < class T > 
void DequeArray<T>::enqueueDeque(T* item) 
{ 
    if (sz == max_queue) 
     arrayResize(2*max_queue); 

    front = front - 1; 
    if (front == max_queue) 
     front = 0; 
    items[front] = item; 
    sz++; 
} 

template < class T > 
T* DequeArray<T>::dequeueDeque() 
{ 
    T* item = NULL; 

    if (!isEmpty()) 
    { 
     item = items[back]; 
     items[back] = NULL; 
     back = back - 1; 
     if (back == front) 
      back = 0; 
     sz--; 
    } 
} 

template < class T > 
void DequeArray<T>::arrayResize(int new_size) 
{ 
    T** temp = new T*[new_size]; 
    int j = front; 

    for (int i = 0; i < sz; i++) 
    { 
     temp[i] = items[j]; 
     j++; 
     if (j == max_queue) j = 0; 
    } 

    front = 0; 
    back = sz - 1; 
    max_queue = new_size; 

    delete[] items; 
    items = temp; 
} 
#endif 

StackDeque.h:

#if !defined NULL 
#define NULL 0 
#endif 

#if !defined (STACKDEQUE_H) 
#define STACKDEQUE_H 
#include "DequeArray.h" 
using CSC2110::DequeArray; 
#include "Widget.h" 

template < class T > 
class StackDeque 
{ 
    private: 
     DequeArray<T>* da; 

    public: 
     StackDeque(); 
     ~StackDeque(); 
     void push(T* item); 
     T* pop(); 
     T* peek(); 
     int sz; 
     bool isEmpty(); 
}; 

template < class T > 
StackDeque<T>::StackDeque() 
{ 
    da = new DequeArray<T>; 
} 

template < class T > 
StackDeque<T>::~StackDeque() 
{ 
    da->dequeueAll(); 
} 

template < class T > 
void StackDeque<T>::push(T* item) 
{ 
    da->enqueueDeque(item); 
} 

template < class T > 
T* StackDeque<T>::pop() 
{ 
    T* temp = NULL; 
    if (!da->isEmpty()) 
    { 
     temp->da->enqueueDeque(); 
     return temp; 
    } 
} 

template < class T > 
bool StackDeque<T>::isEmpty() 
{ 
    return sz == 0; 
} 
#endif 

InventoryManager.cpp:

#include "Widget.h" 
#include "StackDeque.h" 
#include "QueueDeque.h" 
#include "DequeArray.h" 
#include "InventoryManager.h" 

namespace CSC2110 
{ 

InventoryManager::InventoryManager(int inventory_choice) 
{ 
    totalProf = 0; 
    choice = inventory_choice; 
    if (inventory_choice == 1) 
    { 
     stack = new StackDeque<Widget>; 
    } 
    else 
    { 
     queue = new QueueDeque<Widget>; 
    } 
} 

InventoryManager::~InventoryManager() 
{ 

} 

void InventoryManager::buyWidgets(double cost, int num_to_buy) 
{ 
    if (choice == 1) 
    { 
     for (int i = 0; i < num_to_buy; i++) 
     { 
      Widget* widget = new Widget(cost); 
      stack->push(widget); 
      delete widget; 
     } 
    } 
    else 
    { 
     for (int i = 0; i < num_to_buy; i++) 
     { 
      Widget* widget = new Widget(cost); 
      queue->enqueue(widget); 
      delete widget; 
     } 
    } 
} 

double InventoryManager::getTotalProfit() 
{ 
    return totalProf; 
} 

double InventoryManager::sellWidgets(double price, int num_to_sell) 
{ 
    double profitThisTransaction = 0; 
    double profitPerSell = 0; 

    if (choice == 1) 
    { 
     if (num_to_sell > stack->sz) 
     num_to_sell = stack->sz; 

     for (int i= 0; i < num_to_sell; i++) 
     { 
      Widget* widget = NULL; 
      widget = stack->pop(); 

      profitPerSell = price - widget->getCost(); 
      profitThisTransaction += profitPerSell; 
      delete widget; 
     } 
    } 

    else 
    { 
     if (num_to_sell > queue->sz) 
      num_to_sell = queue->sz; 

     for (int i= 0; i < num_to_sell; i++) 
     { 
      Widget* widget = NULL; 
      widget = queue->dequeue(); 

      profitPerSell = price - widget->getCost(); 
      profitThisTransaction += profitPerSell; 
      delete widget; 
     } 
    } 

totalProf += profitThisTransaction; 
return profitThisTransaction; 

} 
} 

Widget.cpp:

#include "Widget.h" 

namespace CSC2110 
{ 

Widget::Widget(double user_choice) 
{ 
    cost = user_choice; 
} 

Widget::~Widget() 
{ 

} 

float Widget::getCost() 
{ 
    return cost; 
} 

} 

エラー:

In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
QueueDeque.h: In instantiation of 'void CSC2110::QueueDeque<T>::enqueue(T*) [with T = CSC2110::Widget]': 
InventoryManager.cpp:42:25: required from here 
DequeArray.h:16:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::max_queue' is private 
    int max_queue; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:55:9: error: within this context 
    if (sz == da->max_queue) 
     ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:16:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::max_queue' is private 
    int max_queue; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:57:20: error: within this context 
    da->arrayResize(2*da->max_queue); 
        ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:166:6: error: 'void CSC2110::DequeArray<T>::arrayResize(int) [with T = CSC2110::Widget]' is private 
void DequeArray<T>::arrayResize(int new_size) 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:57:3: error: within this context 
    da->arrayResize(2*da->max_queue); 
^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:19:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::back' is private 
    int back; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:61:11: error: within this context 
    da->back = da->back + 1; 
     ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:19:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::back' is private 
    int back; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:61:22: error: within this context 
    da->back = da->back + 1; 
        ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:19:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::back' is private 
    int back; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:62:15: error: within this context 
    if (da->back == da->max_queue) da->back = 0; 
      ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:16:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::max_queue' is private 
    int max_queue; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:62:15: error: within this context 
    if (da->back == da->max_queue) da->back = 0; 
      ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:19:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::back' is private 
    int back; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:62:42: error: within this context 
    if (da->back == da->max_queue) da->back = 0; 
             ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:17:7: error: 'CSC2110::Widget** CSC2110::DequeArray<CSC2110::Widget>::items' is private 
    T** items; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:63:11: error: within this context 
    da->items[da->back] = item; 
     ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:19:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::back' is private 
    int back; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:63:11: error: within this context 
    da->items[da->back] = item; 
     ^
In file included from InventoryManager.cpp:2:0: 
StackDeque.h: In instantiation of 'T* CSC2110::StackDeque<T>::pop() [with T = CSC2110::Widget]': 
InventoryManager.cpp:65:24: required from here 
StackDeque.h:53:3: error: 'class CSC2110::Widget' has no member named 'da' 
    temp->da->enqueueDeque(); 
^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
QueueDeque.h: In instantiation of 'T* CSC2110::QueueDeque<T>::dequeue() [with T = CSC2110::Widget]': 
InventoryManager.cpp:80:28: required from here 
DequeArray.h:17:7: error: 'CSC2110::Widget** CSC2110::DequeArray<CSC2110::Widget>::items' is private 
    T** items; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:74:19: error: within this context 
    item = da->items[da->front]; 
       ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:74:19: error: within this context 
    item = da->items[da->front]; 
       ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:17:7: error: 'CSC2110::Widget** CSC2110::DequeArray<CSC2110::Widget>::items' is private 
    T** items; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:75:12: error: within this context 
    da->items[da->front] = NULL; 
      ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:75:12: error: within this context 
    da->items[da->front] = NULL; 
      ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:77:13: error: within this context 
    da->front = da->front + 1; 
      ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:77:25: error: within this context 
    da->front = da->front + 1; 
         ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:78:17: error: within this context 
    if (da->front == da->max_queue) da->front = 0; 
       ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:16:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::max_queue' is private 
    int max_queue; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:78:17: error: within this context 
    if (da->front == da->max_queue) da->front = 0; 
       ^
In file included from StackDeque.h:7:0, 
       from InventoryManager.cpp:2: 
DequeArray.h:18:7: error: 'int CSC2110::DequeArray<CSC2110::Widget>::front' is private 
    int front; 
    ^
In file included from InventoryManager.cpp:3:0: 
QueueDeque.h:78:45: error: within this context 
    if (da->front == da->max_queue) da->front = 0; 
+1

エラーメッセージの詳細を投稿してください... – HazemGomaa

+0

申し訳ありませんが、エラーが追加されました。 – NihilisSpecialis

+0

さて、彼らは*プライベートです。そして、それは「プライベート」のことです。あなたが「友人」でない限り、あなたは外部からプライベートメンバーにアクセスすることはできません。 – AnT

答えて

0

エラーはクリアです。 inventoryMnager.cppからプライベートメンバー変数(max_queue、back、frontなど)とプライベートメンバー関数(arrayResize)にアクセスしようとしています...あなたの問題を解決するには、スコープをpublicに変更する必要があります。

+0

これを行う際の問題は、ユーザーが変数を変更できないように、これらの変数をパブリックにすることを想定していないことです。それは動作しますが、私が必要とするものは正しくありません。 – NihilisSpecialis

+0

@NihilisSpecialisそれらのためのパブリックゲッターを作成してください... – HazemGomaa

+0

今私のexeクラッシュ私はbuyWidgets()でウィジェットを作成しようとすると、メモリリークがあると思いますか? – NihilisSpecialis

0

は、1つのクラスのオブジェクトは、別のクラスのプライベートメソッドにアクセスすることはできませんとだけ別のクラスのパブリックメソッドにアクセスすることができます。

+0

パブリックメソッドにはどうすればアクセスできますか? 私は、DequeArray :: QueueDequeのようなものでenqueue()を試みましたが、それは呼び出しに一致する関数がないことを示しています。 – NihilisSpecialis

関連する問題