0

I got a superclass named Obje

#ifndef OBJE_H
#define OBJE_H

#include <QString>

class Obje
{
    public:
    Obje();
    QString name="";
    int id;
    int itemid=0;
    void setItemid(int i);
    virtual void update(){};
};

#endif // OBJE_H

and I got a subclass named Npc

#ifndef NPC_H
#define NPC_H

#include "obje.h"
#include "QString"
#include "QPixmap"
class Npc : public Obje
{
public:

    Npc(QString dir="left");
    QString direction="left";

    QPixmap* spriteSheet = nullptr;
    QPixmap face;
    QPixmap sprites[3];
    void update(int nid);
    QPixmap* getCurrentSprite();
    int currentFrame;
    QString frameDirection;

};

#endif // NPC_H

I also got a class to manage listWidgets

class ListUpdate
{
public:
    ListUpdate();
    static void update(QListWidget *q, vector<Obje>& i);

    private:
    static struct item{
        QString id;
        QString name;
        QPixmap px;
    };

    static QListWidgetItem* newItem(ListUpdate::item &i, int c);
};

#endif // LISTUPDATE_H

When I try to pass object named "npcs" which is type vector"Npc" to ListUpdate::update , it gives this error Passing the vector:

ListUpdate::update(ui->listWidget,npcs);

error:

C:\Qt Projects\RpgEditor\npcscreen.cpp:64: error: C2665: 'ListUpdate::update': none of the 3 overloads could convert all the argument types

Note: I also got 2 other overload functions and I didn't write them here to prevent unnecessary confusion(that's why it says , "none of the 3 overloads")

Why doesn't it accept my subclass ?

Ozan Deniz
  • 27
  • 5

1 Answers1

0

The relationship between a base class and a derived class does not extend to containers (except for a few special cases). std::vector<Npc> is not convertible to std::vector<Obje>. Perhaps you can template the update method like this :

template<class T>
static void update(QListWidget *q, vector<T>& i);

This solution will work for vectors of any type that matches the expected interface of Obje including, but not limited to, types that publicly derive from Obje.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • Ohh I see, I suspected using a container was the problem. Unfortunately I don't know using templates but perhaps I should study that. Thanks – Ozan Deniz Jan 15 '17 at 14:54