不能指向用户定义的类,但我可以直接实例化它

时间:2015-05-09 01:05:10

标签: c++

我有一个名为HUD的课,每当我写

HUD *hud = new HUD(screenwidth,screenHight);

我收到此错误

  

缺少类型说明符 - 假设为int。注意:C ++不支持default-int

但如果我直接实例化它

Hud hud(screenwidth,screenHight);

它工作正常,我该如何解决这个问题 这里是代码的cpp

HUD::HUD(float windowWidth,float windowHeight){

    sf::Font font;
    font.loadFromFile("arial.ttf");
    time.setFont(font);
    time.setColor(Color::White);
    time.setPosition(Vector2f(windowWidth/2,VERTICAL_INDENTATION));
    time.setCharacterSize(FONT_SIZE);

    coinIcon.loadFromFile(COIN_ICON);
    coinDisplay.setTexture(&coinIcon);
    coinDisplay.setSize(Vector2f(20,20));
    coinDisplay.setPosition(Vector2f(0,VERTICAL_INDENTATION));

    coins.setFont(font);
    coins.setColor(Color::White);
    coins.setPosition(Vector2f(HORIZONTAL_INDENTATION,VERTICAL_INDENTATION));
    coins.setCharacterSize(FONT_SIZE);

    numOfCoins = 0;
}


void HUD::startClock(){
    clock.restart();
}

// returns seconds to the nearest digit
int HUD::getTimeElapsed(){
    return (int)(0.5+clock.getElapsedTime().asSeconds());
}

void HUD::incrementCoins(){
    numOfCoins++;
}

int HUD::getNumOfCoins(){
    return numOfCoins;
}
// only accepts positive numbers
std::string HUD::toString(int num){
    std::string str("");
    char digit;
    while(num%10 != 0){
        digit = '0'+num%10;
        str.insert(0,1,digit);
        num /= 10;
    }
    if(str.size() == 0)
        str = "0";
    return str;
}

void HUD::render(RenderWindow *window){
    coins.setString(toString(numOfCoins));
    time.setString(toString((int)(0.5+clock.getElapsedTime().asSeconds())));

    window->draw(coinDisplay);
    window->draw(coins);
    window->draw(time);
}

这是头文件

using namespace sf;

#define COIN_ICON "Textures/coin.png"
#define VERTICAL_INDENTATION 20
#define HORIZONTAL_INDENTATION 20
#define FONT_SIZE 30

class HUD{
    RectangleShape coinDisplay;
    Texture coinIcon;
    Text coins;
    int numOfCoins;

    sf::Clock clock;
    Text time;

    std::string toString(int num);
public:
    HUD(float windowWidth,float windowHeight);
    ~HUD(void);
    void startClock();
    int restartClock();
    void render(RenderWindow *window);
    int getTimeElapsed();
    void incrementCoins();
    int getNumOfCoins();
};

3 个答案:

答案 0 :(得分:0)

你不能制作指针,因为你不是要求指针。

您正在寻找的语法是:

UPDATE MY_EMP
SET SALARY = SALARY + 100
WHERE EMPLOYEE_ID = 198
Error report -
SQL Error: ORA-04091: table PWR_14_15_L_013209985.MY_EMP is mutating, trigger/function may not see it
ORA-06512: at "PWR_14_15_L_013209985.SWAP", line 4
ORA-04088: error during execution of trigger 'PWR_14_15_L_013209985.SWAP'
04091. 00000 -  "table %s.%s is mutating, trigger/function may not see it"
*Cause:    A trigger (or a user defined plsql function that is referenced in
       this statement) attempted to look at (or modify) a table that was
       in the middle of being modified by the statement which fired it.
*Action:   Rewrite the trigger (or function) so it does not read that table.

与...之间的区别:

HUD* hud = new HUD(screenwidth,screenHight);

前者是实例化堆上的对象,而后者是实例化堆栈上的对象。你必须要打电话:

HUD hud;

如果您选择使用第一种方法进行实例化。

答案 1 :(得分:0)

声明指针的正确语法是:

HUD* hud = new HUD(screenwidth,screenHight);

但话说回来,你不应该尽可能使用裸指针。阅读this

修改

我注意到您写道,当您将类型声明为Hud而不是HUD时,它会起作用。我怀疑这里有一个错字。

答案 2 :(得分:0)

找到问题的根源。问题是在项目中我使用了一个名为common的头文件,其中包含项目中的所有头文件,并且在每个cpp文件中都包含公共头文件。由于某种原因,即使HUD是共同的,它仍然无法工作,直到我在头文件中包含HUD.h声明指针HUD

相关问题