2011-06-15 86 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); 
}