tags:

views:

92

answers:

2

I have a media program. i am adding CD,dvd,book info. I managed to get the book info added to the set. I am now adding the CD info. I have most of the Cd info added but i have a seperate function for adding band members. thats where i need help!

I am getting a cast error: error C2440: 'type cast' : cannot convert from 'const Item *const ' to 'CD'

CD.h

#ifndef CD_H
#define CD_H
#pragma once
#include "item.h"

class CD : public Item
{
public:

    CD(const string& theTitle, const string& theBand, const int snumber);
    const string addBandMember(const string& member);
    const int getNumber() const;
    const string getMusician() const;

    const string getBand() const;
    virtual void print(ostream& out) const;
    ~CD();


private:

    string band;
    string musicians;
    string title;
    int number;

};

ostream& operator<<(ostream& out, const CD* cd);

#endif

CD.cpp

const string CD::addBandMember(const string &member)
{

    return this->musicians = member;

}
A: 

In Library::addBandMember you're trying to cast a pointer to a class type
you need to cast to CD* not CD

And you probably don't want the 2 const in

const Item* const musicCD  

since you want to modify the musicCD !

finally your CD::addBandMember method doesn't seem to add a band member but replace it.

f4
the pointer did the trick thank you..
icelated
yes but you need to cast it to a >>pointer<< to a CD. you can't cast a pointer to a CD it makes no sense -- you edited your comment before I could send mine :)
f4
A: 

You need to change the signature of

void Library::addBandMember(const Item* const musicCD, const string& member)

to

void Library::addBandMember(const Item* musicCD, const string& member)

as addBandMember() method does indeed change your Item instance, so Item cannot be const. Then you need to cast it to CD* using dynamic_cast, because it is a downcast.

pajton
problem is CD has its own class it inherits item. So, there is no musicCD.addband since it dont exist in item. it exists in CD class.
icelated
Oops, right. Then you need to cast, prefferably with dynamic_cast.
pajton