简体   繁体   中英

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. 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.

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. It'll be a simple class that stores a QGraphicsScene and also have a getter and setter method.

Header file

#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. In OOP parlance, anything that derives from QObject is-a QObject (just like, say, a Volvo is-a car).

You can return the QGraphicsScene either by reference (preferred) or by a pointer. 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.

#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.

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. One-liners, and especially accessors (getters/setters), should be kept in the header, since they don't add any extra source lines that way. 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. 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. 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.

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). Do not use qmake if you value your sanity. It's very much novice-unfriendly and unnecessary, too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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