我正在设计一个如下所示的数据结构:

我使用重定向输入来获取填充数据结构所需的数据。这是密码。我知道这需要大量的努力,但是重要的事情就在代码的底部。
#include <iostream>
using namespace std;
template <class DT>
class Cell {
protected:
DT* info;
Cell<DT>* right;
public:
Cell();
Cell(const DT& info);
Cell(const DT& info, Cell<DT>* next);
Cell(const Cell<DT>& copy);
void copy(const Cell<DT>& copy);
~Cell();
void operator=(const Cell<DT>& check);
bool isEmpty();
DT getInfo();
Cell<DT>* next();
int size();
DT& find(const DT& key);
DT& infoAt(int position);
void add(const DT& object);
void insertAt(const DT& newObj, int position);
Cell<DT>* setNext(Cell<DT>* next);
DT remove();
DT removeAt(int position);
};
template <class DT>
Cell<DT>::Cell()
{
info = NULL;
}
template <class DT>
Cell<DT>::Cell(const DT& info) {
this->info = new DT(info);
right = NULL;
}
template <class DT>
Cell<DT>::Cell(const DT& info, Cell<DT>* next) {
this->info = new DT(info);
right = next;
}
template <class DT>
Cell<DT>::~Cell() {
if (info != NULL) {
delete info;
info = NULL;
}
if (right != NULL) {
delete right;
right = NULL;
}
}
template <class DT>
void Cell<DT>::copy(const Cell<DT>& copy) {
if (copy.info == NULL) info = NULL;
else info = new DT(*(copy.info));
if (copy.right == NULL) right = NULL;
else right = new Cell<DT>(*(copy.right));
}
template <class DT>
Cell<DT>::Cell(const Cell<DT>& copy) {
this->copy(copy);
}
template <class DT>
void Cell<DT>::operator=(const Cell<DT>& check) {
if (info != NULL) delete info;
if (right != NULL) delete right;
copy(check);
}
template <class DT>
bool Cell<DT>::isEmpty() {
return (info == NULL);
}
template <class DT>
DT Cell<DT>::getInfo() {
return *info;
}
template <class DT>
Cell<DT>* Cell<DT>::next() {
return right;
}
template <class DT>
int Cell<DT>::size() {
if (next == NULL) {
if (info == NULL) return 0;
else return 1;
}
else return 1 + right->size();
}
template <class DT>
DT& Cell<DT>::find(const DT& key) {
if (key == *info) return *info;
return right->find(key);
}
template <class DT>
DT& Cell<DT>::infoAt(int position) {
if (isEmpty()) return NULL;
if (position == 0) return *info;
if (right == NULL) return NULL;
return right->infoAt(position - 1);
}
template <class DT>
void Cell<DT>::add(const DT& object) {
if (info == NULL) info = new DT(object);
else {
Cell<DT>* newList = new Cell<DT>(*info, right);
*info = object;
right = newList;
}
}
template <class DT>
void Cell<DT>::insertAt(const DT& newObj, int position) {
if (position == 0) add(newObj);
else {
if (right == NULL) right = new Cell<DT>(newObj);
else right->insertAt(newObj, position - 1);
}
}
template <class DT>
Cell<DT>* Cell<DT>::setNext(Cell<DT>* next) {
Cell<DT>* temp = this->right;
this->right = (Cell<DT>*) next;
return temp;
}
template <class DT>
DT Cell<DT>::remove() {
DT temp = *info;
delete info;
if (right == NULL) info = NULL;
else {
Cell<DT>* oldnext = right;
info = right->info;
right = right->right;
oldnext->info = NULL;
oldnext->next = NULL
delete oldnext;
}
return temp;
}
template <class DT>
DT Cell<DT>::removeAt(int position) {
if (position == 0) return remove();
return right->removeAt(position - 1);
}
template <class DT1, class DT2>
class CellNode {
protected:
DT1* info;
Cell<DT2>* myCell;
public:
CellNode();
CellNode(DT1 title, DT2* info);
CellNode(DT1 title);
CellNode(DT2* info);
};
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode() {
info = new DT1();
myCell = new Cell<DT2>();
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT1 title, DT2* info) {
this->info = new DT1(title);
myCell = new Cell<DT2>(info[0]);
for (int i = 1; info[i] != NULL; i++) myCell->add(info[i]);
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT1 title) {
this->info = new DT1(title);
myCell = new Cell<DT2>();
}
template <class DT1, class DT2>
CellNode<DT1, DT2>::CellNode(DT2* info) {
this->info = new DT1();
if (myCell == NULL) {
myCell = new Cell<DT2>(info[0]);
}
for (int i = 0; info[i] != NULL; i++) myCell->add(info[i]);
}
template <class DT1, class DT2>
class MasterCell {
protected:
CellNode<DT1, DT2>* myCellNodes;
int cellsFilled;
public:
MasterCell();
MasterCell(int size);
void add(DT1 title, DT2* info);
};
template<class DT1, class DT2>
MasterCell<DT1, DT2>::MasterCell() {
//This can't do anything.
}
template<class DT1, class DT2>
MasterCell<DT1, DT2>::MasterCell(int size) {
myCellNodes = new CellNode<DT1, DT2>[size];
cellsFilled = 0;
}
template<class DT1, class DT2>
void MasterCell<DT1, DT2>::add(DT1 title, DT2* info) {
myCellNodes[0] = new CellNode<DT1, DT2>(title, info);
}
int main() {
MasterCell<char, char*>* master;
char * check = new char[100000];
char* buffer;
int size = 0;
int lineAmount = 0;
int cellCounter = 0;
while (!cin.eof()) {
cin.get(check[size]);
size++;
}
size;
buffer = new char[size - 1];
for (int i = 0; i < size - 1; i++) {
buffer[i] = check[i];
if (buffer[i] == '\n') lineAmount++;
}
delete[] check;
lineAmount++;
cout << size << endl;
for (int i = 0; i < size - 1; i++) {
cout << buffer[i];
}
master = new MasterCell<char, char*>(lineAmount);
cout << endl << lineAmount << endl;
char* test = new char[5];
test = "hello";
char** test2 = new char*[2];
test2[0] = new char[5];
test2[0] = "hello";
test2[1] = new char[5];
test2[1] = "world";
master->add((*test), test2);
return 0;
}我遇到的问题与MasterCell的add方法有关。首先,我在myCellNodes的初始化器中创建了MasterCell数组。接下来,我希望使用MasterCell的add方法将节点添加到该数组中,但当我试图编译时会得到一条错误消息,而在我的生命中,我不知道为什么。
这句话给我带来了麻烦:
myCellNodes[0] = new CellNode<DT1, DT2>(title, info);这是我得到的错误:
错误C2679二进制“=”:没有找到任何操作符,它接受'CellNode *‘类型的右操作数(或者没有可接受的转换)
注意:大部分垃圾都是我解释重定向输入的方式,这样我就不需要处理调整数组的问题了。
发布于 2016-03-10 06:04:13
您可能希望保留一个动态的指针数组:
CellNode<DT1, DT2>* myCellNodes;
应:
CellNode<DT1, DT2>** myCellNodes;和
myCellNodes = new CellNode<DT1, DT2>[size];应该是
myCellNodes = new CellNode<DT1, DT2> *[size];https://stackoverflow.com/questions/35908689
复制相似问题