C++ 将向量写入新文本文件

C++ 将向量写入新文本文件,c++,vector,text-files,C++,Vector,Text Files,我试图将数据从我的向量写入一个新的文本文件,并在这个过程中创建新的文本文件,下面是我的代码,但我遇到了一个错误,即>b>>c>>d>>e_as_string) { if(e_as_string==“--”) { sun=0.0; } 其他的 { std::istringstream buffer2(e_as_字符串); 如果(!(缓冲区2>>太阳)) { sun=0.0; } } 天气对象={a,b,c,d,e}; 天气数据。推回(objName); } } 计数++; } myfile.clo

我试图将数据从我的向量写入一个新的文本文件,并在这个过程中创建新的文本文件,下面是我的代码,但我遇到了一个错误,即>b>>c>>d>>e_as_string) { if(e_as_string==“--”) { sun=0.0; } 其他的 { std::istringstream buffer2(e_as_字符串); 如果(!(缓冲区2>>太阳)) { sun=0.0; } } 天气对象={a,b,c,d,e}; 天气数据。推回(objName); } } 计数++; } myfile.close(); 对于(自动it=data_weather.begin();it!=data_weather.end();++it) { it->ANSU temp=it->c\U数据+it->d\U数据/2; } 对于(自动it=data_weather.begin();it!=data_weather.end();++it) {
std::cout ans_temp似乎您没有重载流和操作符的
,您是否提供了一个
操作符共享天气类的骨架。现在您重载了@shivakumar,我更新了这个问题好吧,我是新来的,所以上面的代码会放在…std::of stream fs(“newdata.txt”);之前吗(vector::const_iterator it=data_weather.begin();it!=data_weather.end();++it){FS@ JayLAD,这是一个函数定义。所以它不能在另一个函数中。你可以在你的类之后放在main之前。你应该用谷歌来C++操作符重载。谢谢,我会……IVE在main之前把这个函数定义包含在正确的地方,但是我仍然得到以前的错误。把它放进去。成员后的ide结构declaration@shivakumar我这样做了,我得到了一个错误&operator为这个操作符函数声明了太多的参数,而且原始错误仍然存在?
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;
}
friend  ostream &operator << (std::ostream &f, Weather& obj)
    {
    f<<obj.a_data;  //Etc ...code corresponding to dispaly parameters
    return f;
    }
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';
}