C++代码-在同一类的构造函数中调用一个类的构造函数



我正在尝试编译这段C++代码,不幸的是,我未能编译下面的代码。你能帮我解释一下为什么我会犯这个错误吗?

#include <iostream>
#include <cstring>
using namespace std;
class student
{
private:
char name[10];
int id;
int fee;
public:
student(char name[10],int id)
{
strcpy(this->name,name);  //string copy
this->id=id;
fee=0;
}
student(char name[10],int id,int fee)
{
student::student(name,id); //calling a constructor of one class 
// within a constructor of same class
this->fee=fee;
}
void show() //print function
{
cout<<"Name:"<<name<<endl;
cout<<"id:"<<id<<endl;
cout<<"fee:"<<fee<<endl<<endl;
}
};
int main()
{
student s1("DAVID",123);
student s2("WILLIAM",124,5000);
s1.show();
s2.show();
return 0;
}

以下是GCC抱怨的错误main.cpp|20|error: cannot call constructor 'student::student' directly [-fpermissive]|

正如@sweenish正确建议的那样,您必须在初始化部分进行构造函数委派。类似这样的东西:

#include <iostream>
#include <cstring>
using namespace std;
class student
{
private:
char name[10];
int id;
int fee;
public:
student(char name[10],int id)
{
strcpy(this->name,name);  //string copy
this->id=id;
fee=0;
}
student(char name[10],int id,int fee): student(name, id) // constructor delegation
{
this->fee=fee;
}
void show() //print function
{
cout<<"Name:"<<name<<endl;
cout<<"id:"<<id<<endl;
cout<<"fee:"<<fee<<endl<<endl;
}
};
int main()
{
student s1("DAVID",123);
student s2("WILLIAM",124,5000);
s1.show();
s2.show();
return 0;
}

还有,给你一个建议。您可以定义更专业的构造函数,并将不太专业的构造函数的责任委托给更专业的。

#include <iostream>
#include <cstring>
using namespace std;
class student
{
private:
char name[10];
int id;
int fee;
public:
student(char name[10],int id, int fee): id{id}, fee{fee} // more specialized
{
strcpy(this->name,name);  //string copy
}
student(char name[10],int id): student(name, id, 0) { } // less specialized calling the more specialized constructor
void show() //print function
{
cout<<"Name:"<<name<<endl;
cout<<"id:"<<id<<endl;
cout<<"fee:"<<fee<<endl<<endl;
}
};
int main()
{
student s1("DAVID",123);
student s2("WILLIAM",124,5000);
s1.show();
s2.show();
return 0;
}

以下是您的代码,其中包含为使其编译所做的更改;我用注释标记了这些更改。

#include <iostream>
// #include <cstring>  // CHANGED: Prefer C++ ways when writing C++
#include <string>  // CHANGED: The C++ way
// using namespace std;  // CHANGED: Bad practice
class student {
private:
std::string name{};  // CHANGED: Move from C-string to std::string
int id = 0;          // CHANGED: Default member initialization
int fee = 0;
public:
// CHANGED: Move from C-string to std::string
student(std::string name, int id)
: name(name),
id(id)
// CHANGED: ^^ Utilize initialization section
{
// CHANGED: Not needed anymore
//  strcpy(this->name,name);  //string copy
//  this->id=id;
//  fee=0;
}
student(std::string name, int id, int fee) : student(name, id) {
// CHANGED: Not needed anymore
//  student::student(name,id); //calling a constructor of one class
//                            // within a constructor of same class
// NOTE: Inconsistency with other ctor w.r.t. this->
this->fee = fee;
}
void show()  // print function
{
std::cout << "Name:" << name << 'n';
std::cout << "id:" << id << 'n';
std::cout << "fee:" << fee << "nn";
}
};
int main() {
student s1("DAVID", 123);
student s2("WILLIAM", 124, 5000);
s1.show();
s2.show();
return 0;
}

这是相同的代码,但删除了我注释掉的内容,使其更易于阅读。

#include <iostream>
#include <string>
class student {
private:
std::string name{};
int id = 0;
int fee = 0;
public:
student(std::string name, int id) : name(name), id(id) {}
student(std::string name, int id, int fee) : student(name, id) {
this->fee = fee;
}
void show()
{
std::cout << "Name:" << name << 'n';
std::cout << "id:" << id << 'n';
std::cout << "fee:" << fee << "nn";
}
};
int main() {
student s1("DAVID", 123);
student s2("WILLIAM", 124, 5000);
s1.show();
s2.show();
return 0;
}

初始化部分遵循参数列表,并用:进行标记。然后,按照声明的顺序初始化每个成员

在构造函数执行委派的情况下,无法在初始化部分初始化fee。我收到的错误是a delegating constructor cannot have other mem-initializers

我不喜欢这样拆分初始化,如果你坚持为这个类委托构造函数调用,那么实现最具体的构造函数,并用不太具体的构造函数委托给它。我更喜欢默认的成员初始化,因为我认为它可以减少混乱和编写代码。

然后编译代码,得到预期的输出:

Name:DAVID
id:123
fee:0
Name:WILLIAM
id:124
fee:5000

最新更新