C ++指针和对象初始化

时间:2016-03-10 05:55:58

标签: c++ arrays templates pointers data-structures

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

My Data Structure Assignment

我正在使用重定向输入来获取填充数据结构所需的数据。 这是代码。我知道这很重要,但重要的东西就在代码底部附近。

#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的添加方法有关。首先,我在MasterCell的初始化程序中创建了myCellNodes数组。接下来,我想使用MasterCell的add方法向该数组添加节点,除了我在尝试编译时收到错误消息,并且在我的生活中我无法弄清楚原因。

这条线给我带来了麻烦:

myCellNodes[0] = new CellNode<DT1, DT2>(title, info);

这是我得到的错误:

错误C2679二进制'=':找不到哪个运算符采用'CellNode *'类型的右手操作数(或者没有可接受的转换)

注意:main中的很多垃圾是我解释重定向输入的方式,我不需要处理重新调整数组的大小。

1 个答案:

答案 0 :(得分:2)

可能你想保留一个动态的指针数组:

CellNode<DT1, DT2>* myCellNodes;

应该是:

CellNode<DT1, DT2>** myCellNodes;

myCellNodes = new CellNode<DT1, DT2>[size];

应该是

myCellNodes = new CellNode<DT1, DT2> *[size];