简体   繁体   English

C ++分配向量指针

[英]C++ assign vector pointer

I have this vector. 我有这个向量。

vector<CXFileEntity>* menu;// CXFileEntity is class

When i try load an x file to it there is a problem. 当我尝试将x文件加载到其中时,出现了问题。
Here is my code 这是我的代码

menu->at(0)=menu->at(0).LoadXFile(CUtility::GetTheCurrentDirectory()+"\\meshes\\Menu\\background.x", 1, menu->at(0), d3dx->d3ddev);
menu->at(1)=menu->at(1).LoadXFile(CUtility::GetTheCurrentDirectory()+"\\meshes\\Menu\\start.x", 1, menu->at(1), d3dx->d3ddev);

I get these errors 我得到这些错误

Error   1   error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CXFileEntity *' (or there is no acceptable conversion)    c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\window.cpp   86

Error   2   error C2664: 'CXFileEntity::LoadXFile' : cannot convert parameter 3 from 'CXFileEntity' to 'CXFileEntity *' c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\window.cpp   87

It seems to be that when I'm assigning a pointer to my vector it doesn't work the error says that CXFileEntity can't be converted to CXFileEntity* but i assigned it to be a pointer. 似乎是当我将指针分配给矢量时,它不起作用,该错误表明CXFileEntity无法转换为CXFileEntity*但我将其分配为指针。
So, I tried to change it to. 因此,我尝试将其更改为。

menu->at(0)=menu->at(0).LoadXFile(CUtility::GetTheCurrentDirectory()+"\\meshes\\Menu\\background.x", 1, &menu->at(0), d3dx->d3ddev);// added &

but now i got this error 但是现在我得到了这个错误

Error   2   error C2664: 'CXFileEntity::LoadXFile' : cannot convert parameter 3 from 'CXFileEntity' to 'CXFileEntity *' c:\users\owner\documents\visual studio 2010\projects\monopoly\monopoly\window.cpp   87

I'm so lost with this one.. EDIT 2: 我很迷失这一点。 编辑2:
Here is the CXFileEntity class 这是CXFileEntity类

#pragma once
#include "stdafx.h"
/*
    This class represents an x file animation
    It loads the .x file and carries out the animation update and rendering
*/
class CXFileEntity
{
private:
    LPDIRECT3DDEVICE9 m_d3dDevice; // note: a pointer copy (not a good idea but for simplicities sake)

    // Direct3D objects required for animation
    LPD3DXFRAME                 m_frameRoot;
    LPD3DXANIMATIONCONTROLLER   m_animController;
    D3DXMESHCONTAINER_EXTENDED* m_firstMesh;

    // Bone data
    D3DXMATRIX *m_boneMatrices;
    UINT m_maxBones;

    // Animation variables
    unsigned int m_currentAnimationSet;
    unsigned int m_numAnimationSets;
    unsigned int m_currentTrack;
    float m_currentTime;
    float m_speedAdjust;

    // Bounding sphere (for camera placement)
    D3DXVECTOR3 m_sphereCentre;
    float m_sphereRadius;

    std::string m_filename;

    void UpdateFrameMatrices(const D3DXFRAME *frameBase, const D3DXMATRIX *parentMatrix);
    void UpdateSkinnedMesh(const D3DXFRAME *frameBase);
    void DrawFrame(LPD3DXFRAME frame) const;
    void DrawMeshContainer(LPD3DXMESHCONTAINER meshContainerBase, LPD3DXFRAME frameBase) const;
    void SetupBoneMatrices(D3DXFRAME_EXTENDED *pFrame/*, LPD3DXMATRIX pParentMatrix*/); 
    bool Load(const std::string &filename);
public:
    CXFileEntity(LPDIRECT3DDEVICE9 d3dDevice);
    ~CXFileEntity(void);
    mutable D3DXMATRIX m_combinedMat, * s;
    void CreateRay();
    mutable LPD3DXMESH pDrawMesh;
    bool draw;
    float distanceToCollision;
    BOOL hasHit;

    CXFileEntity* LoadXFile(const std::string &filename,int startAnimation, CXFileEntity *menus, LPDIRECT3DDEVICE9 d3ddev);
    void FrameMove(float elapsedTime,const D3DXMATRIX *matWorld);

    void Render() const;
    void SetAnimationSet(unsigned int index);
    void SetComb(LPDIRECT3DDEVICE9 d3dDevice, D3DXMATRIX world);
    float m_fHitDist;
    CXFileEntity *m_pChild;
    CXFileEntity *m_pSibling;

    void NextAnimation();
    void AnimateFaster();
    void AnimateSlower();

    D3DXVECTOR3 GetInitialCameraPosition() const;
    unsigned int GetCurrentAnimationSet() const {return m_currentAnimationSet;}
    std::string GetAnimationSetName(unsigned int index);
    std::string GetFilename() const {return m_filename;}
};

LoadXFile() seems to be returning a pointer, ie. LoadXFile()似乎正在返回一个指针,即。 CXFileEntity * while the vector only accepts objects of CXFileEntity. CXFileEntity *而向量仅接受CXFileEntity的对象。

I don't understand your design. 我不明白你的设计。 You have instances of CXFileEntity in your std::vector . 您的std::vectorCXFileEntity实例。 Then you access those instances and call LoadXFile() on them... which return pointers to CXFileEntity , which you're trying to store back inside the std::vector ? 然后,您访问这些实例并在它们上调用LoadXFile() ...这将返回指向CXFileEntity指针,您正在尝试将其存储回std::vector

Following your own flow, 按照自己的流程,

You need to use the dereference operator * to obtain access to the actual object that the pointer points to. 您需要使用解引用运算符*来访问指针所指向的实际对象。

menu->at(0)=*(menu->at(0).LoadXFile(CUtility::GetTheCurrentDirectory()+
"\\meshes\\Menu\\background.x", 1, &(menu->at(0)), d3dx->d3ddev));

The second error says that you need to pass in a pointer to a CXFileEntity , as you guessed. 第二个错误表明您需要传递一个指向CXFileEntity的指针。 The problem was operator precedence, so you need to use appropriate parentheses. 问题是运算符优先级,因此您需要使用适当的括号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM