Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ N-tree、shared_ptr和“不能声明字段”tnode::data为抽象类型Figure_C++_Shared Ptr_Weak Ptr - Fatal编程技术网

C++ N-tree、shared_ptr和“不能声明字段”tnode::data为抽象类型Figure

C++ N-tree、shared_ptr和“不能声明字段”tnode::data为抽象类型Figure,c++,shared-ptr,weak-ptr,C++,Shared Ptr,Weak Ptr,我最近开始学习面向对象编程,现在我面临的问题是用普通指针将程序更改为使用共享ptr的程序。 我试着用容器n树创建一个程序,它包含两种类型的图形:六边形和菱形。我还必须使用共享的ptr。 第一个问题是: 例如,我有一个函数,它将一个元素添加到n-树: tnode *add(tnode **node, const Figure figure) { tnode *pnext = NULL; tnode *p = (tnode *)malloc(sizeof(tnode)); p->data =

我最近开始学习面向对象编程,现在我面临的问题是用普通指针将程序更改为使用共享ptr的程序。 我试着用容器n树创建一个程序,它包含两种类型的图形:六边形和菱形。我还必须使用共享的ptr。 第一个问题是: 例如,我有一个函数,它将一个元素添加到n-树:

tnode *add(tnode **node, const Figure figure) {
tnode *pnext = NULL;
tnode *p = (tnode *)malloc(sizeof(tnode));
p->data = rhombus;
p->parent = NULL;
p->prev = NULL;
p->next = NULL;
p->child = NULL;
if (*node == NULL)
    *node = p;
else if ((*node)->child == NULL) {
    p->parent = *node;
    (*node)->child = p;
} else {
    pnext = (*node)->child;
    while (pnext->next != NULL)
        pnext = pnext->next;
    p->parent = *node;
    p->prev = pnext;
    pnext->next = p;
}
return p;
}
…或破坏树:

void destroy(tnode **node) {


if (*node == NULL)
    return;
if ((*node)->next != NULL)
    destroy(&(*node)->next);
if ((*node)->child != NULL)
    destroy(&(*node)->child);
free(*node);
*node = NULL;
}
使用std::shared\u ptr会是什么样子? 第二个问题是编译器没有看到Figure是其他两个Figure的父类:

图h

    class Figure {
public:
    virtual bool operator==(const Figure& right);
    virtual &Figure::operator=(const Figure& right);
    virtual double Square() = 0;
    virtual void Print() = 0;
    virtual ~Figure(){}
};
六边形

#include "Figure.h"

class Hexagon : public Figure{
public:
Hexagon();
Hexagon(std::istream &is);
Hexagon(float side);
Hexagon(const Hexagon& orig);

bool operator==(const Hexagon& right);
Hexagon& operator=(const Hexagon& right);

double Square() override;
void   Print() override;

virtual ~Hexagon();
//private:
int side;
};
菱形h

#include <cstdlib>
#include <iostream>
#include "Figure.h"

class Rhombus : public Figure{
Rhombus();
Rhombus(std::istream &is);
Rhombus(int side, float angle);
Rhombus(const Rhombus& orig);

bool operator==(const Rhombus& right);
double Square();// override;

Rhombus& operator=(const Rhombus& right);

virtual ~Rhombus();
//private:
int side;
float angle;
};
在函数add和destroy中,如果参数是图形,编译器也会写入 无法将字段地物声明为抽象类型地物

最后一个问题。据我所知,在N-树结构中,结构的一部分拥有其他部分,因此需要磨损。是否存在错误

struct tnode {
Figure data;
std::weak_ptr<tnode>parent;
std::weak_ptr<tnode>prev;
std::shared_ptr<tnode>next;
std::shared_ptr<tnode>child;
~tnode();
};
那么,你能告诉我,我错在哪里以及如何解决这个问题吗。

在tnode结构中,你声明了Figure类的一个实例,因为它是抽象的,所以不能这样做。你需要使用指针


与Add函数相同的事情,通过一个值,它是不能通过抽象的方式传递的。图中的指针使用的不太像C++或OOP。有没有理由使用C++新的和删除操作符?因为如果你使用Maloc和Frac,构造函数和析构函数实际上不会被调用,而L可能会被调用。不好的东西。@JoachimPileborg在add:std::shared_ptrp=new tnode;?我需要在函数中使用Figure*Figure?在结构Figure*data?@MichaelPister是的,你永远不能有一个抽象类的实例,它包括诸如值传递之类的东西。