0

I have the following class:

  class BandMember
{
private:
    char *name;
    int age;
    int relationshipStatus;
    char *musicianType;
public:
    //functions
    void setName(char* name1);
    char * getName() const;
    void setAge(int age1);
    int getAge() const;
    void setRelationshipStatus(int status);
    int getRelationshipStatus() const;
    void setMusicianType(char* job);
    char* getMusicianType() const;
    BandMember* readBandMember();
    //print
    void print();

    //constructor
    BandMember(const char *nameToAdd = "no name", const int ageToAdd = 0, 
        const int relationshipStatusToAdd = 0, 
        const char *musicianTypeToAdd = "no music type");

    //destructor
    ~BandMember() { delete[] name; delete[] musicianType;}

    //copy constructor
    BandMember(const BandMember& bandMember);
};

in BandMember* BandMember::readMember the code is:

 BandMember* BandMember::readBandMember()
{
    char *nameNewMember, *musicTypeNewMember;
    int ageNewMember, familyStatusNewMember;

    nameNewMember = new char[NAME_LENGTH];
    musicTypeNewMember = new char[NAME_LENGTH];

    cin.ignore(10,'\n');
    cout << "Enter band member name: ";   //askName() - TODO
    cin.getline(nameNewMember, NAME_LENGTH);
    reallocate(nameNewMember, strlen(nameNewMember));

    cout << "enter the age of the band member: ";  //askAge - TODO
    cin >> ageNewMember;

    cin.ignore(10,'\n');
    cout << "enter musician type: ";     //askMusicianType - TODO
    cin.getline(musicTypeNewMember, NAME_LENGTH);
    reallocate(musicTypeNewMember, strlen(musicTypeNewMember));

    return new BandMember(nameNewMember, ageNewMember,,musicTypeNewMember);

the problem is in the return value. The compiler says "missing expression, overload +1": but when I put return new BandMember(nameNewMember, ageNewMember,0,musicTypeNewMember); the code works fine... I don't understand why. I defined a default constractor with default values so why it's not working when I return it with a missing value ?

Yoni Melki
  • 205
  • 3
  • 16

2 Answers2

3

It seems you misunderstand how default arguments works in C++...

You can't skip arguments and have them be the default value. If you want to provide a later argument then you need to provide all arguments in front of it.

Therefore BandMember(nameNewMember, ageNewMember,,musicTypeNewMember) with the double-comma is not correct, you must provide the relationshipStatusToAdd argument as well:

return new BandMember(nameNewMember, ageNewMember,0,musicTypeNewMember);
//                                                ^
// Added argument here --------------------------/
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

I defined a default constractor with default values so why it's not working when I return it with a missing value ?

Default values for parameters only apply starting from the right. You cannot leave out one in the middle.

new BandMember(nameNewMember, ageNewMember,,musicTypeNewMember);
                                 //        ^  NOPE!

For example you can call it like this

new BandMember(nameNewMember, ageNewMember);

If you want a constructor to accept only parameters for the first two and the last argument, then you need to write a constructor where those appear first.

I don't understand why

There is probably no technical limitation that forbids this. Your way of using ,, would perhaps be a way this could work. However, thats not how it is defined in C++.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185