我有以下课程:
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);
};
在
BandMember* BandMember::readMember
中的代码是: 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);
问题出在返回值上。
编译器说“缺少表达式,重载+1”:
但是当我放
return new BandMember(nameNewMember, ageNewMember,0,musicTypeNewMember);
该代码工作正常...
我不明白为什么。
我用默认值定义了一个默认承包商,所以当我返回一个缺少值时为什么它不起作用?
最佳答案
似乎您误解了默认参数在C ++中的工作方式...
您不能跳过参数并将其设置为默认值。如果要提供后面的参数,则需要在其前面提供所有参数。
因此,带双逗号的BandMember(nameNewMember, ageNewMember,,musicTypeNewMember)
是不正确的,还必须提供relationshipStatusToAdd
参数:
return new BandMember(nameNewMember, ageNewMember,0,musicTypeNewMember);
// ^
// Added argument here --------------------------/
关于c++ - 构造函数中缺少表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59036147/