C++ 공부중인데, 내가 쓴 코드가 제대로 움직이고 있나 테스트 하려고 출력 기능을 넣고 있었거든
for문을 i = 0 과 같이 정수로 썼을 땐 출력을 시킬 수 있겠는데
iterator를 이용해서 출력 시키려고 하니까 생각대로 잘 안 돼서 방식을 어떻게 바꿔야 하는지 가르쳐줬으면 함
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
struct HumanInfo{
string name;
string number;
vector<string> email;
};
int main(){
string line, word;
vector<HumanInfo> human;
while(getline(cin, line)){
HumanInfo info;
istringstream record(line);
record >> info.name;
record >> info.number;
while (record >> word)
info.email.push_back(word);
human.push_back(info);
}
for (auto iter = human.begin(); iter != human.end(); iter++){
cout << "Name : " << *iter.name << endl;
}
/*
for (int i = 0; i < human.size(); i++){
cout << "Name : " << human[i].name << endl;
}
*/
}
오류 메시지는 class __gnu_cxx::__normal_iterator <HumanInfo*, std::vector<HumanInfo>> has no member named 'name' 이라고 떠
iter->name 을 쓰던가 (*iter).name으로 해야될걸
. 이 *보다 우선순위가 높아서
아하~ 내가 적은식이면 *(iter.name)처럼 작용하는거구나! 고마워!!! - dc App
에러메시지 읽는 법도 익숙해지면 좋음 지금 메시지가 딱 iter에 멤버name이 없다는 뜻이니까