【发布时间】:2017-05-08 13:30:42
【问题描述】:
我是 C++ 的初级,我正在制作程序,通过在我的课堂上转换为方法来显示一些数据。它还没有真正完成,但是当我创建一个类对象时,它在“Passport Info”行对我说“没有匹配的调用函数”。这是我的代码:
#include <iostream>
#include <string>
using namespace std;
class Passport {
private:
int _size = 6;
std::string* class_people;
std::string* class_birth;
std::string people[6] = {
"bro1", "bro2", "bro3", "bro4", "bro5", "bro6"
};
std::string birth[6] = { "1995", "1994", "1996", "1994", "2001", "1990" };
public:
Passport(std::string people, std::string birth)
{
class_people = new string[_size];
class_birth = new string[_size];
for (int ix = 0; ix < _size; ix++) {
class_people[ix] = people[ix];
class_birth[ix] = birth[ix];
}
}
void show_data()
{
for (int px = 0; px < _size; px++) {
cout << class_people[px] << endl;
cout << class_birth[px] << endl;
}
}
};
int main()
{
setlocale(LC_ALL, "Russian");
Passport Info;
Info.show_data();
system("pause");
return 0;
};
我还想在 Passport 类的私有部分创建指向我的字符串变量的指针,以便在构造函数中使用,但是如何?谢谢。
【问题讨论】:
-
如果你想构造一个
Passport的实例而不传递任何参数,你需要为它定义一个默认构造函数。 -
@CaptainObvlious:回答这个问题。
-
首选
std::vector<std::string>而不是new String[]。通过const &获取参数。将您的私人默认设置为const。 -
@CaptainObvlious:谢谢,它成功了。 :)
-
@MooingDuck:好的,我会的。我只是新人,所以记住一切并不容易。 :)
标签: c++ class pointers dynamic constructor