简体   繁体   English

Qt C++ 重载分辨率选择删除操作符'='

[英]Qt C++ Overload resolution selected deleted operator '='

So I've just started with c++ as I need to make a game with QT for school.所以我刚刚开始使用 c++,因为我需要为学校制作 QT 游戏。 But I still haven't got my head around pointers and so I'm running into a bit of trouble.但是我还没有完全理解指针,所以我遇到了一些麻烦。 I think this problem is to do with pointers but it could actually be to do with QT.我认为这个问题与指针有关,但实际上可能与 QT 有关。

I've numerous scenes in this project and so I want to keep track of which one is currently active.我在这个项目中有很多场景,所以我想跟踪哪个场景当前处于活动状态。 To do this I've created a class RoomController as each scene is a reperesentation of a room.为此,我创建了一个 class RoomController,因为每个场景都是房间的再现。 It'll be a simple class that stores a QGraphicsScene and also have a getter and setter method.这将是一个简单的 class 存储 QGraphicsScene 并且还有一个 getter 和 setter 方法。

Header file Header文件

#ifndef ROOMCONTROLLER_H
#define ROOMCONTROLLER_H
#include <QGraphicsScene>

class RoomController:
{
private:
    QGraphicsScene currentRoom;
public:
    RoomController();
    QGraphicsScene getRoom() const;
    void setRoom(QGraphicsScene room);
};

#endif // ROOMCONTROLLER_H

Source file源文件

#include "RoomController.h"
#include <RoomA.h>
#include <QGraphicsScene>
RoomController::RoomController()
{
    currentRoom = new RoomA();
}

char RoomController::getRoom() const
{
    return currentRoom;
}

void RoomController::setRoom(QGraphicsScene room)
{
    currentRoom=room;
}

You cannot return QObject s by value.您不能按值返回QObject In OOP parlance, anything that derives from QObject is-a QObject (just like, say, a Volvo is-a car).在 OOP 的说法中,任何从QObject派生的东西都是QObject (就像沃尔沃是汽车一样)。

You can return the QGraphicsScene either by reference (preferred) or by a pointer.您可以通过引用(首选)或指针返回QGraphicsScene Since you also intend to set the scene of a room, you have to decide who owns the scene - are you passing the ownership to the room, or is someone else owning it?由于您还打算设置房间的场景,因此您必须决定谁拥有场景 - 您是将所有权传递给房间,还是其他人拥有它? In Qt, you can use a QObject to optionally manage this ownership, ie you can let the RoomController take ownership of the scene if it's parentless, but you have to make the controller a QObject first - and you'd likely want to do that anyway, since in Qt most "controllers" would need to be QObject s to take advantage of the event system, timers, etc.在 Qt 中,您可以使用QObject来有选择地管理此所有权,即如果没有父级,您可以让RoomController获得场景的所有权,但是您必须首先将 controller 设为QObject - 而且您可能无论如何都想这样做,因为在 Qt 中,大多数“控制器”需要是QObject才能利用事件系统、计时器等。

#pragma once // simpler than the #ifdef juggle
#include <QGraphicsScene>

class RoomController : public QObject
{
    Q_OBJECT
    QGraphicsScene *currentRoom = nullptr;
public:
    RoomController(QObject *parent = nullptr) : QObject(parent) {}
    QGraphicsScene &getRoom() const { return *currentRoom; }
    void setRoom(QGraphicsScene *room) {
        Q_ASSERT(room); // this function is only meant to be used with a valid room
        if (currentRoom && currentRoom->parent() == this)
            delete currentRoom;
        currentRoom = room;
        if (!currentRoom->parent())
            currentRoom->setParent(this);
    }
};

That way, if the currentRoom is owned by the RoomController , it'll automatically get deleted by QObject::~QObject() when the controller gets destroyed.这样,如果currentRoomRoomController拥有,它会在 controller 被销毁时自动被QObject::~QObject()删除。

Side note: Putting all those tiny one-line methods into a separate .cpp file will usually only make the source code harder to comprehend, since there's vastly more of it in spite of it doing very little.旁注:将所有这些微小的单行方法放入单独的.cpp文件通常只会使源代码更难理解,因为尽管它做的很少,但它的内容要多得多。 One-liners, and especially accessors (getters/setters), should be kept in the header, since they don't add any extra source lines that way.单行代码,尤其是访问器(getter/setter),应该保存在 header 中,因为它们不会以这种方式添加任何额外的源代码行。 Also, in simple projects it's fine to also keep the short methods within the class declarations in the header files - again, to make the project's structure simpler.此外,在简单的项目中,也可以在 header 文件中的 class 声明中保留短方法 - 再次使项目的结构更简单。 You're, after all, writing a rather simple project that would probably be best served by being kept in a single .cpp file anyway, since it'll be <2000 lines.毕竟,您正在编写一个相当简单的项目,无论如何最好将其保存在单个.cpp文件中,因为它的行数少于 2000 行。 For coursework, it's usually desirable to just start with a main.cpp , put everything there, and only start splitting it up into files once you are well past the 1500 line mark, otherwise you'll spend more time managing files and keeping track of what's where than doing the work you're really supposed to be doing.对于课程作业,通常希望从main.cpp开始,将所有内容放在那里,并且只有在超过 1500 行标记后才开始将其拆分为文件,否则您将花费更多时间来管理文件和跟踪除了做你真正应该做的工作之外,还有什么比做的。

For Qt coursework, a good starting point for a project is a main.cpp that starts with #include <QtWidgets> , and ends with #include "main.moc" , and a CMakeLists.txt that is minimal for the job (5-6 lines at most if all you want is a Qt C++ project with no other dependencies).对于 Qt 课程作业,项目的一个良好起点是main.cpp#include <QtWidgets>开头,以#include "main.moc" ,以及一个CMakeLists.txt对工作来说是最小的(5-如果您想要的只是 Qt C++ 项目,则最多 6 行,没有其他依赖项)。 Do not use qmake if you value your sanity.如果您重视自己的理智,请不要使用 qmake。 It's very much novice-unfriendly and unnecessary, too.这对新手非常不友好,也没有必要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM