2011-06-15 70 views
0

我有一个名为actorVector的矢量,它存储了一个类型为actorManager的对象数组。C++指向一个对象矢量的指针,需要访问属性

actorManager类有一个私有属性,它也是一个GLFrame类型的对象。它有一个访问器getFrame(),它返回一个指向GLFrame对象的指针。

我已经将actorVector的指针传递给了一个函数,所以它是一个指向actorManager类型对象向量的指针。

我需要通过GLFrame对象作为参数传递给这个函数:

modelViewMatrix.MultMatrix(**GLFrame isntance**); 

我目前一直在努力做它是这样,但IM没有得到任何结果。

modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame()); 

任何想法?

+0

你得到一个编译器错误? – 2011-06-15 00:18:20

+2

这是一个好主意,以显示相关的声明,因为这样的描述不是很好,描述性。 – 2011-06-15 00:20:47

回答

3

假设MultMatrix需要一个ActorManager通过或通过参考(如由指针相对),然后要这样:

modelViewMatrix.MultMatrix(*((*actorVector)[i].getFrame())); 

注意,优先级规则意味着以上等同于:

modelViewMatrix.MultMatrix(*(*actorVector)[i].getFrame()); 

然而,这就是你已经有了,所以必须有你不告诉我们的东西......

+0

他说'ActorVector是一个矢量',而不是指针,'* actorVector'是什么意思?我同意我们需要更多信息。 – Nemo 2011-06-15 00:23:31

+0

@Nemo:OP说:“我已经把actorVector的指针传给了函数”... – 2011-06-15 00:25:43

+0

是的,我错过了。 – Nemo 2011-06-15 00:30:21

0

尝试modelViewMatrix.MultMatrix(*(*p)[i].getFrame());

#include <vector> 
using std::vector; 

class GLFrame {}; 
class actorManager { 
    /* The actorManager class has a private attribute, which is also an 
    object of type GLFrame. It has an accessor, getFrame(), which returns 
    a pointer to the GLFrame object. */ 
private: 
    GLFrame g; 
public: 
    GLFrame* getFrame() { return &g; } 
}; 

/* I need to pass the GLFrame object as a parameter to this function: 
    modelViewMatrix.MultMatrix(**GLFrame isntance**); */ 
class ModelViewMatrix { 
public: 
    void MultMatrix(GLFrame g){} 
}; 
ModelViewMatrix modelViewMatrix; 

/* I have a vector called actorVector which stores an array of objects of 
type actorManager. */ 
vector<actorManager> actorVector; 

/* I have passed a pointer of actorVector to a function, so its a pointer 
to a vector of objects of type actorManager. */ 
void f(vector<actorManager>* p, int i) { 
/* I need to pass the GLFrame object as a parameter to this function: 
    modelViewMatrix.MultMatrix(**GLFrame isntance**); */ 
    modelViewMatrix.MultMatrix(*(*p)[i].getFrame()); 
} 

int main() { 
    f(&actorVector, 1); 
}