我根据用户选择实例化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;
答案 0 :(得分:0)
一个类对象无法访问另一个类的私有方法,只能访问另一个类的公共方法。
答案 1 :(得分:0)
错误很明显。您正尝试从inventoryMnager.cpp访问私有成员变量(max_queue,back,front等)和私有成员函数(arrayResize)...将其范围更改为public应解决您的问题。