我有这个 vector 。

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

当我尝试将x文件加载到其中时,出现了问题。
这是我的代码
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);

我得到这些错误
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

似乎是当我将指针分配给我的 vector 时不起作用,该错误提示CXFileEntity无法转换为CXFileEntity*,但我将其分配为指针。
因此,我尝试将其更改为。
menu->at(0)=menu->at(0).LoadXFile(CUtility::GetTheCurrentDirectory()+"\\meshes\\Menu\\background.x", 1, &menu->at(0), d3dx->d3ddev);// added &

但是现在我得到了这个错误
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

我迷失了这个..
编辑2:
这是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()似乎正在返回一个指针,即。 CXFileEntity *,而 vector 仅接受CXFileEntity的对象。

我不明白你的设计。您的CXFileEntity中有std::vector的实例。然后,您访问这些实例并在其上调用LoadXFile() ...,这将返回指向CXFileEntity的指针,您正尝试将其存储回std::vector内部?

按照您自己的流程,

您需要使用取消引用运算符*来访问指针所指向的实际对象。

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

第二个错误表明,您需要像猜到的那样传递指向CXFileEntity的指针。问题是运算符优先级,因此您需要使用适当的括号。

08-27 19:51