我有一个奇怪的问题。我正在使用名为 shape 的自定义类,它具有球体、长方体等继承的形状......
所有这些形状都存储在
vector <Shape*> shape;
一切进展顺利,我可以验证数据是否正确输入。但是,当输入新形状时,以前输入的所有相同类型的形状现在都与最新成员相同。
举个例子:
一个简单的输入文件可以是
0 0 0 1 1 1
1
1
0 1 1 1 1 2
0 1 0 0 0.75 1
0 1 0 0 -1.25 1
0 1 10 10 10 1
在第一个形状之后
0 0 0 1 1 1
1
1
0 1 1 1 1 2
之后是相同的
0 0 0 1 1 1
2
1
0 1 0 0 0.75 1
0 1 0 0 0.75 1
然后它继续发散。
矢量所在的文件在 ShapeContainer.cpp 和 ShapeContainer.H 中,如果您认为另一个文件中的问题是什么,请询问。
非常感谢您的所有帮助。
问候,
约翰
#include "ShapeContainer.H"
#include <iostream>
#include <vector>
void ShapeContainer::PrintT3D (ostream & out, ShapeContainer & SC)
{
// Initialize and Assign Counters //
unsigned int Vertex_ID = 1;
unsigned int Curve_ID = 1;
unsigned int Patch_ID = 1;
unsigned int Surface_ID = 1;
unsigned int Shell_ID = 1;
unsigned int Region_ID = 1;
for (unsigned int i = 0; i < SC.shape.size(); i++)
{
out << "## Shape " << i + 1 << ": " << SC.shape[i]->get_shape_name() << endl;
SC.shape[i]->prepare_t3d_input(Vertex_ID, Curve_ID, Patch_ID, Surface_ID, Shell_ID, Region_ID, 0);
out << *SC.shape[i];
}
}
void ShapeContainer::PrintStat3D (ostream & out, ShapeContainer & SC)
{
vector <double> LocalCoords;
vector <double> LocalProps;
// Start printing updated Stat3D file //
out << SC.BoundingBoxDimensions << endl;
out << SC.shape.size() << endl;
out << SC.NumberModes(SC) << endl;
for (unsigned int i = 0; i < SC.shape.size(); i++)
{
LocalCoords = SC.shape[i]->get_coords();
LocalProps = SC.shape[i]->get_properties();
out << SC.shape[i]->get_shape_type() << " " << SC.shape[i]->get_mode() << " " << LocalCoords << " " << LocalProps << endl;
}
}
void ShapeContainer::ThrowOut(ShapeContainer & SC)
{
for (unsigned int i = 0; i < SC.shape.size(); i++)
{
if ((SC.shape[i]->InBoundingBox(SC.BoundingBoxDimensions))==false) {
SC.shape.erase(SC.shape.begin()+i-1);
i=i-1;
}
}
}
unsigned int ShapeContainer::NumberModes (ShapeContainer & SC)
{
vector <int> ModesList;
ModesList.resize(SC.shape.size(),0);
for (unsigned int i=0;i<ModesList.size();i++) {ModesList[i]=SC.shape[i]->get_mode();}
sort (ModesList.begin(), ModesList.end());
unsigned int count=1;
for(unsigned int i=0;i<ModesList.size()-1;i++)
{
if(ModesList[i]!=ModesList[i+1])
count++;
}
return count;
}
形状容器.H
#ifndef _SHAPECONTAINER_H_
#define _SHAPECONTAINER_H_
#include <iostream>
#include <vector>
#include <math.h>
#include "utils.H"
#include "Shape.H"
#include "Sphere.H"
#include "Cuboid.H"
#include "Ellipsoid.H"
#include "Octahedron.H"
using namespace std;
class ShapeContainer
{
public:
ShapeContainer(){};
~ShapeContainer(){};
void initialize();
/// Friend of Print Functions ///
void PrintT3D (ostream & out, ShapeContainer & SC);
void PrintStat3D (ostream & out, ShapeContainer & SC);
void ThrowOut(ShapeContainer & SC);
/// Private access functions ///
void set_BoundingBox(vector <double> BB) {BoundingBoxDimensions = BB;}; // Cooresponds to box at (x,y,z) with dims (dx,dy,dz)
unsigned int NumberModes (ShapeContainer & SC);
/// Vector-Like Functions ///
void push_back(Shape* NewShape) {shape.push_back(NewShape);}
private:
vector <double> BoundingBoxDimensions;
vector <Shape*> shape;
};
#endif