无法在c ++中打印指针的正确值

时间:2016-12-05 03:58:12

标签: c++ pointers vector

很长一段时间以来,我没有玩指针,我认为我卡住了

所以我有两个节点和三角形的类,我试图创建一个有四个三角形的方形(底部,右边,顶部,左边)

一个正方形由5个节点组成,一个三角形由3个节点指针组成

这是我简化的代码

#include <iostream>
#include <math.h>
#include <vector>

using namespace std;


class Node {
public:
    float Xx = 0.0; // nodes(i).X
    float Xy = 0.0;
    float xx = Xx; // nodes(i).x
    float xy = Xy;
    vector<float> v;
    float m = 0.0;
    vector<float> f;
    bool fixed = false;

    Node() {
        Xx = 0.0;
        Xy = 0.0;
    }

    Node(float x, float y) {
        Xx = x;
        Xy = y;

        v.push_back(0.0);
        v.push_back(0.0);

        f.push_back(0.0);
        f.push_back(0.0);

        //cout << "the size of f : " << f.size() << endl;

    }


};

    class Triangle {
    public:
        Node *a;
        Node *b;
        Node *c;
        vector<float> stress;

        Triangle(Node *a1, Node *b1, Node *c1) {
            a = a1;
            b = b1;
            c = c1;
            stress.push_back(0.0);
            stress.push_back(0.0);
            stress.push_back(0.0);
            stress.push_back(0.0);
        }
    };


    vector<Node> nodes;
    vector<Triangle> tris;

void createSq(float x, float y, float side) {
    float Ax = x; // -1 
    float Ay = y; // -1
    float hside = side;
    Node A = Node(Ax, Ay);
    Node B = Node(Ax + 2.0 * hside, Ay);
    Node C = Node(Ax, Ay + 2.0* hside);
    Node D = Node(Ax + 2 * hside, Ay + 2 * hside);
    Node E = Node(Ax + hside, Ay + hside);

    Node *pA = &A; // pA is a pointer to the Node A, Triangle containts only the pointers to the nodes
    Node *pB = &B;
    Node *pC = &C;
    Node *pD = &D;
    Node *pE = &E;

    //cout << " A: " << A << " B : " << B << endl;

    nodes.push_back(A);
    nodes.push_back(B);
    nodes.push_back(C);
    nodes.push_back(D);
    nodes.push_back(E);

    tris.push_back(Triangle(pA, pC, pE));
    tris.push_back(Triangle(pA, pB, pE));
    tris.push_back(Triangle(pB, pD, pE));
    tris.push_back(Triangle(pC, pD, pE));
}


int main(int argc, char* argv[]) {
    float nTiles = 1;
    float hside = 1 / nTiles;

    createSq(-1.0, -1.0, hside);

    for (int i = 0; i < nodes.size(); i++) {
        cout << "the value of Xx: " << nodes[i].Xx << " and the Xy: " << nodes[i].Xy << endl;
    }

    cout << " the value of Xx of tris : " << tris[0].a->Xx << endl;

    while (1) {

    }
    return 0;

}

执行代码后,结果如下:

Result

如您所见,如果我尝试从节点向量访问Xx的值,它将正确显示数字。 但是,如果我从tris向量访问它,它会输出一个非常大的数字

任何人都知道这里有什么问题?似乎我错过了什么

更新:

根据评论,我稍微改变了我的代码

    tris.push_back(Triangle(&nodes[nodes.size() - 5], &nodes[nodes.size() - 3], &nodes[nodes.size() - 1]));
    tris.push_back(Triangle(&nodes[nodes.size() - 5], &nodes[nodes.size() - 4], &nodes[nodes.size() - 1]));
    tris.push_back(Triangle(&nodes[nodes.size() - 4], &nodes[nodes.size() - 2], &nodes[nodes.size() - 1]));
    tris.push_back(Triangle(&nodes[nodes.size() - 3], &nodes[nodes.size() - 2], &nodes[nodes.size() - 1]));

现在它有效,但是,我会尝试使用列表来改变它,因为我听说使用向量是有风险的

0 个答案:

没有答案