C ++为什么我的struct成员用上下文改变值?

时间:2015-03-04 19:44:13

标签: c++ struct

我遇到了一些奇怪的行为,我不确定是什么导致它。

根据我访问结构成员的上下文,我得到了非常不同的值。

以下是定义结构的相关代码:

struct ScreenTriangle
{
    int type;
    Vector2 points[3];
    Vector4 verts[3];
    Vector4 norms[3];

    ScreenTriangle() {}
    ScreenTriangle(Triangle, Matrix, int, int);
    bool split(ScreenTriangle&);
};

struct Vector2
{
    double x;
    double y;
    ... //Lots of functions
};

struct Vector4
{
    double x;
    double y;
    double z;
    double w;
    ... //Lots of functions
};

在ScreenTriangle结构的构造函数结束时,我添加了一些调试代码来打印出值:

ScreenTriangle::ScreenTriangle(Triangle triangle, Matrix matrix, int width, int height)
{
    ... //Initialization logic
    //DEBUG
    fprintf(stderr, "\tConstruct Triangle\n");
    fprintf(stderr, "\t\tpoints\n");
    fprintf(stderr, "\t\t\t%f, %f\n", points[0].x, points[0].y);
    fprintf(stderr, "\t\t\t%f, %f\n", points[1].x, points[1].y);
    fprintf(stderr, "\t\t\t%f, %f\n", points[2].x, points[2].y);
}

这是在构造函数中完成的最后一件事,所以我认为这些是正确的值。以下是实际创建ScreenTriangles的代码:

...
ScreenTriangle first = ScreenTriangle(triangle, m_cam[best_image], image_width, image_height);
//DEBUG
fprintf(stderr, "\tScreenTriangle pre\n");
fprintf(stderr, "\t\tPoints\n");
fprintf(stderr, "\t\t\t%f, %f\n", first.points[0].x, first.points[0].y);
fprintf(stderr, "\t\t\t%f, %f\n", first.points[1].x, first.points[1].y);
fprintf(stderr, "\t\t\t%f, %f\n", first.points[2].x, first.points[2].y);
    if(first.type == OTHER_TRIANGLE)
    {
        ScreenTriangle second;
        first.split(second); //Modifies the ScreenTriangle calling the function and instantiates a new ScreenTriangle on the passed reference
        //DEBUG
        fprintf(stderr, "\tScreenTriangle post top\n");
        fprintf(stderr, "\t\tPoints\n");
        fprintf(stderr, "\t\t\t%f, %f\n", first.points[0].x, first.points[0].y);
        fprintf(stderr, "\t\t\t%f, %f\n", first.points[1].x, first.points[1].y);
        fprintf(stderr, "\t\t\t%f, %f\n", first.points[2].x, first.points[2].y);
        fprintf(stderr, "\tScreenTriangle post bottom\n");
        fprintf(stderr, "\t\tPoints\n");
        fprintf(stderr, "\t\t\t%f, %f\n", second.points[0].x, second.points[0].y);
        fprintf(stderr, "\t\t\t%f, %f\n", second.points[1].x, second.points[1].y);
        fprintf(stderr, "\t\t\t%f, %f\n", second.points[2].x, second.points[2].y);
        ...

正如您在构造函数完成后可以立即看到的那样,我再次打印出数据。我希望这些是相同的,但绝对不是。我想也许数据已经被破坏了,所以我在split方法的中间添加了一些输出来输出分割前三角形的值,以及分割得到的两个三角形的值。然后我还从主程序中输出这些值,只是为了检查结果。结果远非理智。

//stderr, NOT CODE, couldn't figure out how to format it otherwise
Construct Triangle
    points
        470.142394, 436.976659
        470.353823, 437.546349
        470.366987, 440.896491
ScreenTriangle pre
    Points
        0.000776, 4734553876061397887934347591916838705351250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        28381368350.827759, -19400122721264338832825401151226542897600390000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        0.000000, 0.000000
Split triangle
    Original
        points
            470.142394, 436.976659
            470.353823, 437.546349
            470.366987, 440.896491
        ...
    new_point
        470.175036, 437.546349
    ...
    Top Triangle
        points
            470.142394, 436.976659
            470.175036, 437.546349
            470.353823, 437.546349
    Bot Triangle
        points
            470.175036, 437.546349
            470.353823, 437.546349
            470.366987, 440.896491
ScreenTriangle post top
    Points
        0.000776, 4734553876061397887934347591916838705351250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        -29450462837259105633893654055197308700574950000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000, -19400122721262697099929384820740204292408740000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        28381368350.836658, -19400122721264338832825401151226542897600390000000000000000000000000000000000000000000000000000000000000000000000000000.000000
ScreenTriangle post bottom
    Points
        -29450456430284346305088130197738746199887030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000, -19400122721262697099929384820740204292408740000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        28381368350.836658, -19400122721264338832825401151226542897600390000000000000000000000000000000000000000000000000000000000000000000000000000.000000
        0.000000, 0.000000

我对发生的事情感到很困惑。为什么当我检查ScreenTriangle结构中的值是他们的一件事时,当我从结构外部检查下一个命令中的值时,它们会不同吗?

当我从ScreenTriangle结构中的函数再次检查它们时,它们显示原始值显然没有改变,但是当我立即再次检查它们时,从结构外部,我得到了这些荒谬的结果。

我想知道它是否与我的系统有关。我在Windows 7机器上通过Cygwin编译并运行它,所以我将代码拖到运行Fedora的机器上(我认为是v20)并再次尝试,但结果却相同。

我在这里缺少什么?为什么会这样?

2 个答案:

答案 0 :(得分:0)

在定义向量之前使用向量。 Vector2和Vector4的定义应该在ScreenTriangle的定义之前。这可能会导致问题

答案 1 :(得分:0)

如果你改变了它会修复它吗?

ScreenTriangle first = ScreenTriangle(triangle, m_cam[best_image], image_width, image_height);

要:

ScreenTriangle first(triangle, m_cam[best_image], image_width, image_height);

也许Vector2中有一些东西可以防止结构的成员副本正常发生(原始代码)。