gpt4 book ai didi

C++ 指针和对象初始化

转载 作者:搜寻专家 更新时间:2023-10-31 02:17:14 25 4
gpt4 key购买 nike

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

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 中的很多垃圾是我以一种不需要处理调整数组大小的方式来解释重定向的输入。

最佳答案

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

CellNode<DT1, DT2>* myCellNodes;

应该是:

CellNode<DT1, DT2>** myCellNodes;

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

应该是

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

关于C++ 指针和对象初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com