2013-04-10 2 views
0

내 벡터의 데이터를 새 텍스트 파일에 쓰려고 시도하는 중, 프로세스에서 새 텍스트 파일을 만드는 중입니다. 아래 코드는 내 코드입니다. < < 그냥 오류가 있습니다. FS 후, 오류 상태가 더 연산자는 "< <"는 < < 연산자를 오버로드하지 않은 것 같다 이러한 피연산자새 텍스트 파일에 벡터 작성

struct Weather 
{ 
    int a_data; 
    int b_data; 
    double c_data; 
    double d_data; 
    double e_data; 
    double ans_temp; 
}; 


ofstream &operator << (std::ofstream &f, Weather& obj) 
{ 
f<<obj.a_data;///Etc ...code corresponding to dispaly parameters 
return f; 
}; 

int main() 
{ 
    using std::vector; 
    using std::string; 
    using std::getline; 
    using std::cout; 

    vector<Weather> data_weather; 
    string line; 
    ifstream myfile ("weatherdata.txt"); 

    if (myfile.is_open()) 
    { 
    int count = 0; 
    while (getline(myfile, line)) 
    { 
     if (count > 6) 
     { 
      int a, b; 
      double c, d, e; 
      std::istringstream buffer(line); 
      std::string e_as_string; 
      if (buffer >> a >> b >> c >> d >> e_as_string) 
      { 
       if (e_as_string == "---") 
       { 
        sun = 0.0; 
       } 
       else 
       { 
        std::istringstream buffer2(e_as_string); 
        if (!(buffer2 >> sun)) 
        { 
         sun = 0.0; 
        } 
       } 
       Weather objName = {a, b, c, d, e}; 
       data_weather.push_back(objName); 
      } 
     } 
     count++; 
    } 
    myfile.close(); 

    for (auto it = data_weather.begin(); it != data_weather.end(); ++it) 
    { 
     it->ans_temp = it->c_data + it->d_data /2; 
    } 
    for (auto it = data_weather.begin(); it != data_weather.end(); ++it) 
    { 

     std::cout << it->ans_temp << std::endl; 
    } 

    std::ofstream fs("newdata.txt"); 
      for(vector<Weather>::const_iterator it = data_weather.begin(); it != data_weather.end(); ++it) { 
      fs << *it << '\n'; 
     } 
    } 
    else 

    cout << "unable to open file"; 

    scat::pause("\nPress <ENTER> to end the program."); 

    return 0; 
} 
+3

당신은'Weather'에 대한'연산자 <<'과부하를 제공 한 적이 있습니까? –

+0

은 Weather 클래스의 스케 leiton을 공유합니다. 그리고 ow는 << 연산자를 오버로드 했습니까? – shivakumar

+0

@shivakumar ive 님이 질문에 대한 답변을 – jaylad

답변

0

일치하지 않습니다.

friend ostream &operator << (std::ostream &f, Weather& obj) 
    { 
    f<<obj.a_data; //Etc ...code corresponding to dispaly parameters 
    return f; 
    } 
+0

으로 변경했습니다. 위의 코드는 this 앞에 놓이게됩니다 .... std :: ofstream fs ("newdata.txt"); (벡터 :: const_iterator it = data_weather.begin(); it! = data_weather.end(); ++ it) { fs << * it << '\ n'; – jaylad

+0

@jaylad 이것은 함수 정의입니다. 그래서 다른 기능을 할 수 없습니다. 수업이 끝난 후 메인에 배치 할 수 있습니다. 그리고 당신은 C++ 연산자 오버로딩을 위해 google을 사용해야한다. –

+0

고마워, 내가 할거야 ... 필자는 main 앞에 정확한 위치에이 함수 정의를 포함 시켰지만 여전히 원래의 에러를 얻고 있습니까? – jaylad

0
ofstream& operator<< (std::ofstream &f, const Weather& obj) 
             ^^^^ 
{ 
    f<<obj.a_data;///Etc ...code corresponding to dispaly parameters 
    return f; 
}; 

또는 (권장하지 않음)

for(vector<Weather>::iterator it = data_weather.begin(); it != data_weather.end(); ++it) 
        ^^^^ 
{ 
    fs << *it << '\n'; 
} 
관련 문제