我有这段代码:
class Passport
{
public:
Passport()
{
std::vector<std::string> class_people(people,people+6);
std::vector<std::string> class_birth(birth,birth+6);
}
void show_data() {
std::copy(class_people.begin(), class_birth.end());
}
};
当我尝试在class_people
中使用show_data()
时,编译器会抱怨该变量未被声明。
答案 0 :(得分:0)
如果您希望所有成员函数都能访问变量,则需要将其作为类的成员变量:
html {
font-size: 18px;
}
h1 {
font-size: 1rem;
}
@media only screen and (min-device-width: 700px) {
html {
font-size: 19px;
}
h1 {
font-size: 1.5rem;
}
}
当然,您的构造函数需要进行一些调整(我假设class Passport {
std::vector<std::string> class_people;
std::vector<std::string> class_birth;
public:
Passport():
class_people(people,people+6),
class_birth(birth,birth+6)
{}
};
和people
是全局的,就像在您的代码示例中一样)。
答案 1 :(得分:0)
假设构造函数采用初始化参数
class Passport {
std::vector<std::string> class_people;
std::vector<std::string> class_birth;
public:
Passport(const char* people[], const char* birth[])
: class_people(people,people+sizeof(people)/sizeof(people[0]))
, class_birth(birth,birth+sizeof(birth)/sizeof(birth[0]))
{}
};