"Extern"对象问题:错误:Id 返回 1 个退出状态

"Extern" object problem: error: Id returned 1 exit status

本文关键字:状态 退出 返回 错误 Extern 对象 问题 Id      更新时间:2023-10-16

我是Qt的新人。我试着做贪吃蛇游戏。它几乎完成了,但我在编写分数增加功能时遇到了问题。我正在使用"extern"命令以便将一个函数用于另一个类,但Qt给了我一个错误

我不能尝试任何事情,因为我不明白这个问题。(collinding_items代码没有问题。它可以在没有分数的情况下运行。问题是关于"外部"命令(

主.cpp

Score *score = new Score();
scene->addItem(score);

得分.h

类分数:公共QGraphics文本项 {

公共:

Score(QGraphicsItem *parents=0);
void increase();
int getScore();

私人:

int 游戏得分=0; };

得分.cpp

void Score::increase()
{
Gamescore=+20;
setPlainText(QString("Score: ") + QString::number(Gamescore));
}
int Score::getScore()
{
return Gamescore;
}

蛇.cpp

extern Score *score;

QList<QGraphicsItem *> colliding_items = collidingItems();
for(int i=0, n=colliding_items.size(); i<n; ++i)
{
if(typeid(*colliding_items[i])==typeid (Fruit))
{
scene()->removeItem(colliding_items[i]);
delete(colliding_items[i]);
score->increase();
}
}

对"分数"的未定义引用

错误:ID 返回 1 个退出状态

(找不到文件集合2.exe(

我发现了问题。它源自"extern"命令的使用。它必须用作全局。您可以在下面看到我的代码部分。

得分.h

class Score: public QGraphicsTextItem
{
public:
Puan(QGraphicsItem *parents=0);
void increase();
int getScore();
private:
int Gamescore;
};

蛇.cpp

extern Score *scoring;
Snake::Snake()
{
}
void Show()
{
scoring->increase();
}

如果我使用 extern 作为全局,那么我可以在 Snake 中使用 Score.cpp 的 increase(( 函数.cpp

但我还没有学会"如何提出问题以及如何向这个网站添加代码"。