gpt4 book ai didi

c++ - 类名没有命名类型

转载 作者:太空狗 更新时间:2023-10-29 20:42:40 25 4
gpt4 key购买 nike

我有两个类 GameApButton,我希望 ApButton 使用 Game 属性,所以我想使这两个 friend 功能,但我不断收到错误:

`Game` does not name a type 

我知道我不应该在游戏类中添加 apbutton.h 但我必须这样做,因为游戏使用 ApButton (一个从按钮继承的类)你呢对于这个问题还有其他解决方案吗?

这是两个类的代码:

#ifndef GAME_H
#define GAME_H

#include <QtGui>
#include <QWidget>
#include <apbutton.h> //I have to add this
#include <QHBoxLayout>
#include <QTimer>
#include <iostream>
#include <QMouseEvent>

using namespace std;

namespace Ui {
class Game;
}

friend class ApButton;

class Game : public QWidget
{
Q_OBJECT
public:
explicit Game(QWidget *parent = 0);
~Game();
QLabel *bomb_label();
private:
Ui::Game *ui;
ApButton **btn; //that's why I have to include apbutton.h
};

#endif // GAME_H


#ifndef APBUTTON_H
#define APBUTTON_H

#include <QPushButton>
#include <iostream>
#include <QMouseEvent>
#include <game.h>

using namespace std;

class ApButton : public QPushButton
{
Q_OBJECT
public:
explicit ApButton(QWidget *parent = 0);
void setRowCol(int _row,int _col);
void mousePressEvent(QMouseEvent *ev);
private:
string name;
int row;
int col;
Game g; //here is the problem!
};

#endif // APBUTTON_H

最佳答案

我假设 Ui::Game 是您的 Qt 生成的小部件类,而 Game 是您的实现类。您的问题是循环包含依赖性(在“Game.h”和“ApButton.h”之间),通常使用前向声明来解决。事实上,您已经在“Game.h”中的 Ui::Game 类中使用该机制:

namespace Ui {
class Game;
}

现在只需在下面添加:

class ApButton;

并删除:

#include <apbutton.h>

除非您不打算在“Game.h”头文件中使用ApButton 的任何方法并且btn 仍然是一个指针成员(这里为什么要使用双指针?),你可以接受不完整的类型。

也是你的 friend 声明

friend class ApButton;

属于 Game 类。

关于c++ - 类名没有命名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17898717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com