扩展精灵类的重载难度.这怎么错了



所以我一直在制作一款益智游戏,使用的是我之前使用过的精灵系统,在我过去的项目中扩展类时也很好,但是我的代码总是抛出一个错误

错误C2511:'Door:: Door(float,float,float,float,float,float, float,int,CGame *)':在"门"中找不到重载的成员函数

和我不知道为什么,因为我已经检查了我以前的项目中的所有类,以及它们如何与程序的其余部分接口,它是相同的,但这个新类仍然出错。在我的main中创建对象的代码抛出了"no overloaded function in Door takes 8 arguments"。

门类:

Door.h

#pragma once
#include "sprite.h"
class CGame;
class Door :
    public CSprite
{
public:
    Door(void);
    Door(float, float, float, float,float, float, float, int, CGame * p_Game);
    void Update(float dt);
    ~Door(void);
private:
};

Door.CPP

#include "stdafx.h"
#include "Door.h"
#include "Game.h"
Door::Door(void)
{
}
        //So this is where i try to make door class extend sprite. but it keeps saying "overloaded member function not found in Door" 
        //and the other error is "doesnt take 8 args" and to top it off. It says unexpected end of file. 
        //Uncomment this block and the code in the door bit of the map gen to see what it is doing wrong
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game) : CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
{
 m_iType = 4                //sets the type of sprite that this object is.
}
void Door::Update(float dt)
{
}
Door::~Door(void)
{
}

这是我要扩展的Sprite类(只是相关的部分)

Sprite.h

#pragma once
class CGame;

class CSprite  
{
public:
    float m_fX; //the position of the centre of the sprite
    float m_fY;
    float m_fW; //width of the sprite in arbitrary units
    float m_fH; //height of the sprite in arbitrary units
    float m_fvX;
    float m_fvY;
    int m_iTextureID; //which of the preloaded textures to use
    float m_fR; //red component between 0 and 1
    float m_fG; //green component between 0 and 1
    float m_fB; //blue component between 0 and 1
    float m_fA; //alpha value 0-1
    int m_iType;
    CGame * m_pGame;
public:
    CSprite();
    CSprite(float x, float y, float w, float h,float vX,float vY, int textureID ,CGame * p_Game);
    bool bIsCollidingWith( CSprite * othersprite_);
    bool markedForDelete;
//This new constructor is added to the Csprite.h header file.
    float getX() { return m_fX; }
    float getY() { return m_fY; }
    virtual bool TagForDeletion();
    virtual int GetSpriteType();
    virtual ~CSprite();
    virtual void Render();
    virtual void Update(float dt);
}; 

Sprite.cpp

#include "StdAfx.h"
#include <gl.h>
#include <glut.h>
#include <glaux.h>
#include "main.h"
#include "sprite.h"
#include "Game.h"
CSprite::CSprite( )
{
    m_fX=0.0f;
    m_fY=0.0f;
    m_fW=1.0f;
    m_fH=1.0f;
    m_fvX=0.0f;
    m_fvY=0.0f;
    markedForDelete=false;
    m_fR=m_fG=m_fB=m_fA=1.0;
    m_iTextureID=0;
}
CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)
{
    m_iType = 1;
    m_fX=x_;
    m_fY=y_;
    m_fW=w_;
    m_fH=h_;
    m_fvX=vX_;
    m_fvY=vY_;
    m_fR=m_fG=m_fB=m_fA=1.0;
    m_iTextureID=textureID_;
    m_pGame = p_Game;
    markedForDelete=false;
}
CSprite::~CSprite(void)
{
}

游戏类中的执行使用从Sprite扩展的参数来创建对象

p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);

您的float数错了。

声明
Door(float, float, float, float,float, float, float, int, CGame * p_Game);
//     1      2      3      4     5      6      7

定义
Door::Door(float _x, float _y, float _w, float _h, float _vX, float _vY, int _texID, CGame * p_Game)
//           1         2         3         4         5          6 
   : CSprite(_x, _y, _w, _h, _vX, _vY, _texID, p_Game)
使用

p_Door[m_iSpritesLoaded++]=new Door(uiRow,4.58 -uiCol,1,1,0,0,4,this);
//                                    1     2         3 4 5 6

(你真的需要Stack Overflow吗?!)

看起来您的Door构造函数有一个额外的float参数。声明有7个float,而Door.cpp中的定义有6个。

在Door.h中构造函数有9个参数门(float, float,float, float,float, float,float, float, int, CGame * p_Game);而在Door.cpp中,构造函数的定义只有8个元素,也就是说缺少了一个float

解决方案:向构造函数

的定义中添加一个实参

CSprite::CSprite(float x_, float y_, float w_, float h_,float vX_,float vY_, int textureID_, CGame * p_Game)

最新更新