使用扩展精灵类重载难度。这怎么了?

时间:2011-08-09 19:02:29

标签: c++ polymorphism overloading

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

  

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

我不知道为什么我已经检查了我之前项目中的所有类以及它们如何与程序的其余部分进行交互并且它是IDENTICAL但是这个新类仍然是错误的。并且我的main中用于创建对象的代码抛出“在门中没有重载函数需要8个参数”。

门类:

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.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);

3 个答案:

答案 0 :(得分:1)

您错误地计入了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吗?!)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在Door.h中,构造函数有9个参数 门(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)