C++类属性错误值

C++类属性错误值,c++,class,vector,codeblocks,C++,Class,Vector,Codeblocks,编辑:要初始化位置数组m_pos[3],我在构造函数中将其所有值设置为0,然后从主函数调用另一个名为SetPos的函数,该函数仅设置行星在3D地图中的位置: 因此,构造函数采用以下形式: 这样做不好吗?根据需要,我不能直接通过构造函数设置位置 原件: 我创建了一个名为Planet的类,它控制地图中的一系列行星对象。每个对象都有一个数组pos[3],其中存储了必须绘制行星的坐标 行星还拥有一个名为DrawConnections的功能,负责绘制代表实际行星和其他行星之间连接的线条。一颗行星所连接的行

编辑:要初始化位置数组m_pos[3],我在构造函数中将其所有值设置为0,然后从主函数调用另一个名为SetPos的函数,该函数仅设置行星在3D地图中的位置:

因此,构造函数采用以下形式:

这样做不好吗?根据需要,我不能直接通过构造函数设置位置

原件:

我创建了一个名为Planet的类,它控制地图中的一系列行星对象。每个对象都有一个数组pos[3],其中存储了必须绘制行星的坐标

行星还拥有一个名为DrawConnections的功能,负责绘制代表实际行星和其他行星之间连接的线条。一颗行星所连接的行星存储在一个向量std::vector connections中

由于属性是封装的,因此Planet类中有一个函数返回行星的位置,称为GetPosfloat*pos,其中*pos是指向能够存储行星位置的数组的指针

首先,这些是Planet.h文件中的原型和变量声明:

public:
void DrawConnections(float radius);
void GetPos(float* position);

private:
float m_pos[3];
std::vector<Planet> m_connection;
Planet.cpp中的函数GetPos如下所示:

void Planet::DrawConnections(float radius) //parameter radius controls width of lines
{
float position[3]={0.0f,0.0f,0.0f};   //array storing the position of the planets
                                      //which we are connecting to

//various OpenGl calls go here

glBegin(GL_LINES);                    //begins drawing the lines
for(int i=0;i<m_connection.size();i++)  //for each planet we are connected to, draw a
                                        //line
{
    glVertex3f(m_pos[0],m_pos[1],m_pos[2]);  //draws the first point of the line in the 
                                           //actual planet

    m_connection[i].GetPos(position);    //Gets the position of the planet we connect to

    glVertex3f(position[0],position[1],position[2]);  //draws the second point of the
                                                      //in the planet we connect to
}
glEnd();                                             //ends drawing


//some other OpenGl calls

}
void Planet::GetPos(float* position)
{
    position[0]=m_pos[0];                  //copies the data from the position array to 
    position[1]=m_pos[1];                  //the given pointer
    position[2]=m_pos[2];
}
任何行星都有x,z和0坐标。它们中的每一个都有一组x、y、z坐标,x和z始终不同于0

但是,一些对GetPos的调用返回x和z等于0,而另一些调用工作正常

这导致许多线从行星到屏幕左下角,不代表任何连接。根据我所了解的情况,我认为问题出在GetPos中。但是,其他类似的绘图函数也使用GetPos,在调用DrawConnection函数之前调用它们时,它们可以完美地工作,但是调用DrawConnections后,调用它们时似乎会受到影响,这就好像调用时修改了position数组的值,从而干扰了与position相关的所有其他内容,包括她自己

作为补充信息,我正在使用代码块和MinGW-GNU-GCC编译器。非常感谢您能给我的任何帮助。

为什么不呢

public:
void DrawConnections(float radius);
const std::vector<float>& GetPos() const {return m_pos;};

private:
std::vector<float> m_pos;
std::vector<Planet> m_connection;

我强烈建议对位置向量使用简单的结构,而不是原始浮点数组。在并非绝对需要的情况下,应该避免传递指针。此外,如果没有看到你的行星构造器,我们无法判断m_pos向量是否被正确初始化为合理的值。问题是什么还不太清楚。GetPos返回的x和z值等于零有什么问题吗?如果是这样的话,你能检查一下这些值是否在对象本身中被正确设置了吗?不要忘记,您也在3D空间中工作,并且OpenGL相机的位置将影响这些线在屏幕上的显示方式。问题是,对于x、z坐标不等于0的行星,getPos返回的某些值为0。您在向量中按值存储行星。你们是否在另一个地点操纵它们,并可能推动行星处于零位置?嗨,像素化学家,谢谢你们的帮助。不幸的是,这个问题与存储在向量中的行星无关,我仍然不知道为什么,它与坐标本身有关。这并不一定能真正解决问题。
void Planet::GetPos(float* position)
{
    position[0]=m_pos[0];                  //copies the data from the position array to 
    position[1]=m_pos[1];                  //the given pointer
    position[2]=m_pos[2];
}
public:
void DrawConnections(float radius);
const std::vector<float>& GetPos() const {return m_pos;};

private:
std::vector<float> m_pos;
std::vector<Planet> m_connection;