Olof
Olof

Reputation: 797

Class is not a type

I get an error 'RentedMediaItem is not a type. Hoverever it says that when I use the class as a parameter but not when I use is as an object.

inventory.h where I get the error.

#ifndef INVENTORY_H
#define INVENTORY_H

#include <QString>
#include "rentedmediaitem.h"
#include "historypurchase.h"
#include "supportticket.h"

class RentedMediaItem;

class Inventory
{
private:
    RentedMediaItem * RentedMediaItem; // No error
    MediaItem** mediaItems;
    HistoryPurchase* historyPurchase;
    SupportTicket* tickets;
public:
    Inventory();//Not implemented
    void supportTicket(QString Text); //Not implemented
    QString* getAllSupportTickets(); //Not implemented
    QString supportTicket(int index); //Not implemented
    void mediaItem(int index); //Not implemented
    QString* getAllRentedMediaItems(); //Not implemented
    HistoryPurchase* getPurchaseHistory(); //Not implemented
    void useMedia(int index); //Not implemented
    void addMediaItem(RentedMediaItem* rentedMediaItem); //ERROR
};

#endif // INVENTORY_H

rentedmediaitem.h where I declare the RentedMediaItem class.

#ifndef RENTEDMEDIAITEM_H
#define RENTEDMEDIAITEM_H

#include "mediaitem.h"
#include <time.h>

class RentedMediaItem
{
private:
    unsigned int endDate;
    MediaItem* media;

public:
    RentedMediaItem();
    MediaItem* getMedia();
    int getEndDate();
    void initialize(unsigned int time);
    void use();
};

#endif // RENTEDMEDIAITEM_H

Upvotes: 1

Views: 618

Answers (1)

Filip Ros&#233;en
Filip Ros&#233;en

Reputation: 63902

You have stumbled upon a typical case of name hiding.


Since you declare Inventory::RentedMediaItem having the same name as the type itself, lookups following that declaration will find the member-variable, and not the type itself.

There are several ways around the issue, but the most simple approach would be to name your member variable something else (that doesn't conflict with the name of the type).


Example

struct Obj {
  /* .. */
};
int main () {
  Obj Obj;      // declare a variable named `Obj` of type `Obj`
  Obj x;        // ill-formed, `Obj` is the previously declared variable
  struct Obj y; // ok, using an elaborate-type-specifier
}

Upvotes: 6

Related Questions