简体   繁体   English

C++ 指针和对象初始化

[英]C++ Pointer and Object Initialization

I'm designing a data structure that looks like this:我正在设计一个如下所示的数据结构:

我的数据结构分配

I'm using redirected input to get the data needed to fill the data structure.我正在使用重定向输入来获取填充数据结构所需的数据。 Here is the code.这是代码。 I know it's a lot to slog through but the important stuff is near the bottom of the code.我知道要通过很多东西,但重要的东西在代码的底部附近。

#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;
}

The problem I'm having has to do with MasterCell's add method.我遇到的问题与 MasterCell 的 add 方法有关。 First I have created the myCellNodes array in the initializer of MasterCell.首先,我在 MasterCell 的初始化程序中创建了 myCellNodes 数组。 Next, the I want to add nodes to that array using MasterCell's add method except I get an error message when I try to compile and for the life of me I can't figure out why.接下来,我想使用 MasterCell 的 add 方法将节点添加到该数组,除非我在尝试编译时收到错误消息,并且在我的一生中我无法弄清楚原因。

This line is the one that gives me trouble:这一行是给我带来麻烦的:

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

This is the error I get:这是我得到的错误:

Error C2679 binary '=': no operator found which takes a right-hand operand of type 'CellNode *' (or there is no acceptable conversion)错误 C2679 二进制“=”:未找到采用“CellNode *”类型的右侧操作数的运算符(或没有可接受的转换)

Note: A lot of the junk in main is me interpreting the redirected input in such a way that I don't have to deal with re-sizing arrays.注意: main 中的很多垃圾是我以不必处理重新调整数组大小的方式解释重定向的输入。

Probably you want to keep a dynamic array of pointers:可能你想保留一个动态的指针数组:

CellNode<DT1, DT2>* myCellNodes;

should be:应该:

CellNode<DT1, DT2>** myCellNodes;

and

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

should be应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM