ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 类名称未命名类型

2019-08-31 18:07:36  阅读:107  来源: 互联网

标签:c qt


我有两个classess游戏和ApButton,我希望ApButton使用游戏属性,所以我想做这两个朋友的功能,但我一直收到错误:

`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 class ApButton;

属于Game类.

标签:c,qt
来源: https://codeday.me/bug/20190831/1777501.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有