1
我一直在为学校工作,我们必须创建一个客户端类与4字符串,4 int和矢量(int)作为最后一个参数。问题是,当我想要打印矢量的所有元素时,如果直接使用我的增变器,它将打印不存在。C++奇怪的行为变异矢量
vector<int> v_int;
vector<int>::iterator it_v_i;
v_int.push_back(2);
v_int.push_back(3);
v_int.push_back(7);
v_int.push_back(1);
Client client("nom1", "prenom1", "adress1", "city1", "comment1", 1240967102, 44522, 5, 10, v_int);
v_int = client.getIdResources();
for (it_v_i = v_int.begin(); it_v_i != v_int.end(); it_v_i++) {
cout << *it_v_i << ", ";
}
打印2,3,7,1如预期的,但下面的代码
for (it_v_i = client.getIdResources().begin(); it_v_i != client.getIdResources().end(); it_v_i++) {
cout << *it_v_i << ", ";
}
打印未识别号码(如3417664 ...),未识别号码,7,1
我真的不明白为什么会这样
编辑:
构造:
Client::Client(const string& name, const string& surname, const string& adress, const string& city, const string& comment,
const int& phoneNb, const int& postalCode, const int& termAppointment, const int& priority, const vector<int>& idResources)
: name(name), surname(surname), adress(adress), city(city), comment(comment), phoneNumber(phoneNb),
postalCode(postalCode), termAppointment(termAppointment), priority(priority), idResources(idResources)
{ }
的Mutator:
std::vector<int> getIdResources() const{ return idResources; }
那么这是否意味着第一种情况是唯一正确的方法呢? –
@DadEapPurple,是或者修改'getIdResources()'返回'const std :: vector&'。 –
hmjd