无法将指针定义为用户定义的类,但我可以直接实例化
Cannot make a pointer to user defined class but i can directly instantiate it
我有一个名为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();
};
您无法制作指针,因为您不要求一个。
您要寻找的语法是:
HUD* hud = new HUD(screenwidth,screenHight);
那和:
之间的区别HUD hud;
是前者在堆上实例化对象,而后者则在堆栈上实例化对象。您必须确保致电:
delete hud;
如果您选择使用第一个方法实例化。
声明指针的正确语法是:
HUD* hud = new HUD(screenwidth,screenHight);
但是,您不应在可能的情况下使用裸指针。阅读此。
编辑
我注意到您写道,当您将类型声明为Hud
而不是HUD
时,它有效。我怀疑这里有一个错字。
找到了问题的来源。问题在于,在项目中,我使用了一个名为Common的标题,该标题包括项目中的所有标题,并包括每个CPP文件中的共同点。由于某种原因,即使包括HUD包括共同的共同点,它仍然无法使用,直到我将HUD.H纳入标头文件中,宣布指针HUD
相关文章:
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 我可以在运行时重新定义在 OpenCascade/OCCT 标头中定义的 c++ 静态常量吗?
- 在 C++ 中,我可以在不修改类的情况下定义对类的隐式转换吗?
- 当我可以在头文件中声明和定义所有文件时,为什么要创建一个.cpp文件?
- 我可以对 std::array 使用自定义分配器来获取安全加密密钥吗?
- 我可以定义一个 constexpr 匿名/未命名变量吗?
- 在C++中,我可以在定义自己的复制构造函数后跳过定义赋值运算符吗?
- 我可以使用常量定义数组的长度,那么为什么 int d[b] 不起作用呢?
- 我可以为不同类型集定义模板吗
- 如果 std::numeric_limits<float>::is_iec559 为真,这是否意味着我可以以明确定义的方式提取指数和尾数?
- 我可以从样式表 (qss) 更改自定义动态属性吗?
- 我可以在将项目编译为静态库(未定义的引用)时在代码中使用 QImage 吗?
- 我可以依靠 TCHAR 的定义对我正在使用的字符集做出正确的假设吗?
- 我可以将特征定义的变量与函数adjacent_difference一起使用吗?
- 我可以有一个从外部不可见但未在标头中定义的静态友元函数吗?
- 我可以在不同的 TU 中提供相同的函数定义吗?
- 我可以解决unique_ptr在<MyType>仅存储 nullptr 时不需要 MyType 析构函数定义吗?
- 为什么我可以阻止基元的隐式转换,而不是用户定义类型的隐式转换
- 我可以定义一个(键入的)常数,该常数确定不占据可执行文件中的空间
- 我可以获取标准库中定义的函数的地址吗?