Re: A very weird bug......
In message
<3b768e92-a91d-4e5b-81af-4fc268db63b9@n77g2000hse.googlegroups.com>, xz
<zhang.xi.cn@gmail.com> writes
Floor.h:
class Floor{
public :
private:
int m; //# of rows of vertices
int n; //# of columns of vertices
double r;
That's a member variable called "r". I'd recommend choosing a naming
convention that makes it clear when something is a member.
int meshOrder;
std::vector< std::vector<Vertex*> > floorVertices;
public:
Floor(double x0, double y0, double xM, double yM, double r = 1.0, int
meshOrder = 1);
//...
void describe();
Probably ought to be const.
};
in Floor.cpp:
Constructor:
Floor::Floor(double x0, double y0, double xM, double yM, double r,
That's a parameter called "r", local to the implementation of the
constructor. Although it happens to have the same name, it is completely
unrelated to the member variable called "r". The compiler cannot read
your mind.
int
meshOrder) {
m = int ((yM - y0)/r + 1);
n = int ((xM - x0)/r + 1);
You probably need to learn about initialization lists.
floorVertices = *(new vector< vector<Vertex*> >(m, vector<Vertex*>
(n) ) );
floorVertices[0][0] = new Vertex(x0, y0);
for(int j = 1; j < n; j++) {
floorVertices[0][j] = new Vertex(j+x0, y0);
floorVertices[0][j]->addAdjacency(floorVertices[0][j-1]);
}
for(int i = 1; i < m; i++) {
floorVertices[i][0] = new Vertex(x0, i+y0);
floorVertices[i][0]->addAdjacency(floorVertices[i-1][0]);
}
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
floorVertices[i][j] = new Vertex(j+x0, i+y0);
floorVertices[i][j]->addAdjacency(floorVertices[i-1][j]);
floorVertices[i][j]->addAdjacency(floorVertices[i][j-1]);
}
}
}
None of the above initializes the member variable "r", or assigns
anything to it. Its value is therefore undefined.
Floor has a self-describe function as follows:
void Floor::describe() {
cout << "The floor is represented by the mesh as follows: \n"
"(r = " << r << ", meshOrder = " << meshOrder << "\n";
That reports the member variable r, which has never been initialized.
for(int i = 0; i < floorVertices.size(); i++) {
for(int j = 0; j < floorVertices[i].size(); j++) {
floorVertices[i][j]->describe();
}
}
}
In a test file I have lines like:
Floor keck(-5.0, -5.0, 10.0, 10.0, 1.0);
//Floor keck(-5.0, -5.0, 10.0, 10.0);
keck.describe();
In the information printed by keck.describe(), I got:
(r = 2.33637e-310, meshOrder = 0)
With DDD I checked the value of "r" inside the constructor, it is *1*
all the
way to the end of the constructor.
That's the constructor parameter "r". It's not the member variable "r".
I have also tried changing r to public (for debugging), and printed it
out right after the constructor, I found it being 2.36305e-310 there.
Seems like the value is changed at the end of the constructor.
No, the value of the member variable is never set.
And the same applies equally to the two variables called "meshOrder".
--
Richard Herring