形状及其点ID.形状id不同,但点id连续递增

  • 本文关键字:id 不同 但点 连续 形状 ID c++
  • 更新时间 :
  • 英文 :


要求:

Shape ID: 0 (say rectangle)  
Point IDs in rectangle: 0, 1, 2 and 3.

Shape ID: 1 (say square)
Point IDs in square: 0, 1, 2 and 3.

我通过编写了一个代码,用于创建一个形状并为每个形状添加点。定义了类Shape和LBP。

问题:

形状1中的点ID不是从"0"开始的。理想情况下,形状1(正方形(中的点ID应从0开始,而不是从1开始。我觉得这是一个程序,将新创建的点附加到上一个形状对象的点集,即形状0(矩形(。

Shape ID: 0 (say rectangle)
Point IDs: 0
Shape ID: 1 (say square)
Point IDs: 1 

问题:如何使创建的每个新形状的点ID从"0"开始?

定义的类如下所示。

MasterDefine.h

#include<iostream>
using namespace std;

class LBP
{
private:
double  x, y;
int     pointID;
static int pointCount;
public:
void    setLBP(double, double);
void    getLBP(double&, double&);
void    addLBPatEnd();
void    addLBPatLocation();
void    delLBPatEnd();
void    delLBPatLocation(int);
void    setLBPID();
int     getLBPID();
void    goToNode(int);
int     getNoOfPoints();
LBP(double, double);
~LBP();
LBP* next, * prev;
};
class Shape
{
private:
int shapeID;
static int shapeCount;
public:
void setShapeID();
int  getShapeID();
LBP* points;
Shape();
};

主要.cpp

#include"MasterDefine.h"
int LBP::pointCount = 0;
int Shape::shapeCount = 0;
LBP* lbp, *firstPt, *lastPt;
void printLBPForward();
void printLBPReverse();
void delLBP(int);
void delAllLBPs();

int main()
{
cout << endl << "Hello world" << endl;
Shape* shape0 = new Shape;
cout << endl << "Shape id is " << shape0->getShapeID() << endl; //gives id of shape0
LBP* s0p0 = new LBP(22, 33); //point 0 of shape 0
shape0->points = s0p0;
Shape* shape1 = new Shape;
cout << endl << "Shape id is " << shape1->getShapeID() << endl; //gives id of shape1
LBP* s1p0 = new LBP(10, 5);//point 0 of shape 1
shape1->points = s1p0;
cout << endl << "point id is " << s1p0->getLBPID() << endl;

return 1;
}

LBP.cpp

#include"MasterDefine.h"
//#include "LBP.h"
LBP* lbpFirst, *lbpLast;
void LBP::getLBP(double& getX, double& getY)
{
getX = x;
getY = y;
}
void LBP::addLBPatEnd()
{
//  cout << endl << "add lbp at end " << endl;
//  cout << endl << "Pointer value in member function " << this << endl;
LBP* tempNode;
if (getNoOfPoints() == 1)
{
tempNode = this;
tempNode->next = NULL;
tempNode->prev = NULL;
lbpFirst = tempNode;
}
if (getNoOfPoints() == 2)
{
tempNode = this;
lbpFirst->next = tempNode;
lbpFirst->prev = tempNode;
tempNode->next = tempNode->prev = lbpFirst;
lbpLast = tempNode;
}
if (getNoOfPoints() > 2)
{
tempNode = this;
lbpLast->next = tempNode;
tempNode->next = lbpFirst;
lbpFirst->prev = tempNode;
tempNode->prev = lbpLast;
lbpLast = tempNode;
}

//goToNode();
}
void LBP::delLBPatEnd()
{
LBP* tempNodeDel;
tempNodeDel = lbpFirst;
do
{
tempNodeDel = tempNodeDel->next;
} while (tempNodeDel->getLBPID() != pointCount - 2);
cout << endl << "tempNodeDel id is " << tempNodeDel->getLBPID() << endl;
tempNodeDel->next = lbpFirst;
lbpFirst->prev = tempNodeDel;

}
void LBP::delLBPatLocation(int NodePos)
{
LBP* TempNodePosDel, *TempNodePosDelPrev, *TempNodePosDelNext;
TempNodePosDel = lbpFirst;
do
{
TempNodePosDel = TempNodePosDel->next;
} while (TempNodePosDel->getLBPID() != NodePos);
cout<<endl<<"NodeID that is to be deleted is "<<TempNodePosDel->getLBPID()<<endl;
cout << endl << "TempNodePosDel" << TempNodePosDel << endl;


TempNodePosDelPrev = TempNodePosDel->prev;
TempNodePosDelNext = TempNodePosDel->next;
cout << endl << "TempNodePosDelPrev id is " << TempNodePosDelPrev->getLBPID() << endl;
cout << endl << "TempNodePosDel id is " << TempNodePosDel->getLBPID() << endl;
cout << endl << "TempNodePosDelNext id is " << TempNodePosDelNext->getLBPID() << endl;
TempNodePosDelPrev->next = TempNodePosDelNext;
TempNodePosDelNext->prev = TempNodePosDelPrev; 

cout << endl << "************************************" << endl;
cout << endl << "TempNodePosDelPrev " << TempNodePosDelPrev << endl;
cout << endl << "TempNodePosDelPrev->next " << TempNodePosDelPrev->next << endl;
cout << endl << "TempNodePosDelNext->prev " << TempNodePosDelNext->prev << endl;
cout << endl << "TempNodePosDelNext " << TempNodePosDelNext << endl;

cout << endl << "************************************" << endl;
}
void LBP::setLBPID()
{
pointID = pointCount;
}
int LBP::getLBPID()
{
return pointID;
}
void LBP::goToNode(int)
{
}
int LBP::getNoOfPoints()
{
return pointCount;
}
LBP::LBP(double setX, double setY)
{
x = setX;
y = setY;
setLBPID();
pointCount++;
addLBPatEnd();

}
LBP::~LBP()
{
cout << endl << "In destructor " << endl;
int NodePosForDel;
NodePosForDel = this->getLBPID();
if (NodePosForDel != 1)
{
delLBPatLocation(NodePosForDel);
}
/*delLBPatEnd();*/

cout << endl << "this pointer in destructor " << this << endl;
pointCount--;
}

形状.cpp

#include"MasterDefine.h"
void Shape::setShapeID()
{
shapeID = shapeCount;
}
int Shape::getShapeID()
{
return shapeID;
}
Shape::Shape()
{
setShapeID();
shapeCount++;
}

控制台输出:

Shape id is 0
point id is 0
Shape id is 1
point id is 1

有人能帮我吗?我肯定错过了什么。我想不通。

问题:如何使创建的每个新形状的点ID从"0"开始?

由于您的static int pointCount;成员是静态的,它对于class LBP的所有实例都是相同的成员变量,并且由于实例共享该成员,因此在创建新实例时,它可以访问所有以前的实例对此变量所做的更改:

LBP::LBP(double setX, double setY)
{
...
pointCount++;
...

}

这个值绝对不应该静态存储,顶点也不负责存储形状中所有顶点的数量。例如,制作一个非静态变量class Shape,并将该信息保存在那里:

class Shape
{
private:
size_t verticesNum;
...

最新更新