2016-01-23 100 views
-1

我試圖循環遍歷包含對象的列表。我正在使用天氣API讀取數據,並將每個數據成員存儲爲一個對象,然後將每個對象存儲在一個列表中。在我存儲整個列表後,我希望能夠遍歷列表並顯示對象。我正在閱讀Json的數據,我正在使用casablanca軟件包來完成這項工作。我想要做的就是能夠在我的main.cpp中的displayFullForcast函數中遍歷我的列表。 這裏是我的列表定義:在對象列表中循環

list<Weather> weather; 

這裏是我的頭:

​​

這是我實現:

#include "Weather.h" 
#include <list> 
#include <string> 
#include <iostream> 

using std::cout; 
using std::endl; 
using std::cin; 


Weather::Weather(double currentTemperature, double maxTemperature, double minTemperature, 
    string weatherDescription, double humidity, string time) : 
    cityState(cityState), currentTemperature(currentTemperature), highTemperature(highTemperature), lowTemperature(lowTemperature), 
    weatherDescription(weatherDescription), humidity(humidity), time(time) 
{ 
} 
Weather::Weather() 
{ 
    cityState = ""; 
    currentTemperature = 0.0; 
    highTemperature = 0.0; 
    lowTemperature = 0.0; 
    weatherDescription = ""; 
    humidity = 0.0; 
    highestTemperature = 0.0; 
    lowestTemperature = 0.0; 

} 

Weather::~Weather() 
{ 

} 

這裏是我的main.cpp

void displayFullForcast() 
{ 
    list<Weather>weather; 
    for (list<Weather>::iterator it = weather.begin(); it != weather.end(); ++it) 
    { 
     cout << *it << endl; 
    } 

} 

void displayTodayForcast() 
{ 
    list<Weather>weather; 
} 

void displayLowHigh() 
{ 
    list<Weather>weather; 
} 

void displayHumidity() 
{ 
    list<Weather>weather; 
} 


void displayMenu() 
{ 
    int choice = 0; 
    cout << "What option would you like to see:\n" 
     << "(1) Forcast for 5 days\n" 
     << "(2) Forcast for Today\n" 
     << "(3) The Lowest and Highest Temperature\n" 
     << "(4) Humidity for Today\n" << endl; 
    cin >> choice; 
    switch (choice) 
      { 
      case 1: displayFullForcast(); 
       break; 
      case 2: displayTodayForcast(); 
       break; 
      case 3: displayLowHigh(); 
       break; 
      case 4: displayHumidity(); 
       break; 
      } 
} 



void executeWeatherQuery() 
{ 
    http_client client(U("http://api.openweathermap.org")); 
    uri_builder builder(U("/data/2.5/forecast")); 
    builder.append_query(U("q"), U("Searcy,AR")); 
    builder.append_query(U("appid"), U("5ee41aef99a6e283abcdd5a04d89ae67")); 
    builder.append_query(U("units"), U("imperial")); 
    builder.append_query(U("mode"), U("json")); 

    http_response response = client.request(methods::GET, builder.to_string()).get(); 
    web::json::value forecastJson = response.extract_json(true).get(); 
    web::json::value forecastListJson = forecastJson.at(U("list")); 

    if (forecastListJson.is_array()) 
    { 
     for (size_t i = 0; i < forecastListJson.size(); i++) 
     { 
      web::json::value forecastDayJson = forecastListJson[i]; 
      web::json::value mainJson = forecastDayJson.at(U("main")); 
      web::json::value currentTemperatureJson = mainJson.at(U("temp")); 
      double currentTemperature = 0; 
      if (!currentTemperatureJson.is_null()) 
      { 
       currentTemperature = currentTemperatureJson.as_double(); 
      } 
      cout << "Current Temperature " << currentTemperature << endl; 

      web::json::value weatherJson = forecastDayJson.at(U("weather"))[0]; 
      web::json::value mainWeatherJson = weatherJson.at(U("main")); 
      string weatherDescription = ""; 
      if (!mainWeatherJson.is_null()) 
      { 
       weatherDescription = conversions::to_utf8string(mainWeatherJson.as_string()); 
      } 
      cout << "Weather Description " << conversions::to_utf8string(weatherDescription) << endl; 

      web::json::value MainJson = forecastDayJson.at(U("main")); 
      web::json::value minTemperatureJson = MainJson.at(U("temp")); 
      double minTemperature = 0; 
      if (!minTemperatureJson.is_null()) 
      { 
       minTemperature = minTemperatureJson.as_double(); 
      } 
      cout << "Min Temp: " << minTemperature << endl; 

      web::json::value MainMaxJson = forecastDayJson.at(U("main")); 
      web::json::value maxTemperatureJson = MainMaxJson.at(U("temp")); 
      double maxTemperature = 0; 
      if (!maxTemperatureJson.is_null()) 
      { 
       maxTemperature = maxTemperatureJson.as_double(); 
      } 
      cout << "Max " << maxTemperature << endl; 

      web::json::value MainHumidityJson = forecastDayJson.at(U("main")); 
      web::json::value humidityJson = MainHumidityJson.at(U("humidity")); 

      double humidity = 0; 
      if (!MainHumidityJson.is_null()) 
      { 
       humidity = humidityJson.as_double(); 
      } 
      cout << "Humidity: " << humidity << endl; 

      web::json::value MainTimeJson = forecastDayJson.at(U("dt_txt")); 
      string time = ""; 
      if (!MainTimeJson.is_null()) 
      { 
       time = conversions::to_utf8string(MainTimeJson.as_string()); 

       //time = localtime(time); 
      } 
      cout << "Time" << time << endl; 

      Weather WeatherForcast(currentTemperature, maxTemperature, minTemperature, 
       weatherDescription, humidity, time); 
      list<Weather>weather; 
      weather.push_back(WeatherForcast); 
     } 
    } 
} 

void main() 
{ 
    /*string location; 
    cout << "Please enter a city and a State (Example: Searcy,AR)" << endl; 
    cin >> location;*/ 
    executeWeatherQuery(); 


displayMenu(); 
} 

**我是rec發出這個錯誤仍然在線上cout < < * it < < endl; 錯誤:沒有運算符「< <」匹配這些操作數。操作數類型是std :: ofstream和Weather。

+0

我也編輯了我的代碼來修復範圍錯誤。 –

回答

1

您的問題似乎是,在你的每一個功能,您有一個單獨的本地天氣名單。

所以你收集的數據到一個列表被在函數結束破壞,然後顯示剛創建(並且是空的)另一個列表的內容。

擁有而不是一個全球listweather。

1

我沒有安裝特定的天氣API包,但是從你的代碼,它看起來像你剛纔有一個範圍的問題。

在您的executeWeatherQuery中,您聲明list<Weather> weather;,然後執行weather.push_back(WeatherForcast);; weather對象僅在executeWeatherQuery函數中具有本地性,因此只要該函數結束,該對象就會超出範圍,並且其中的數據不再有效。此外,您的其他功能即display列表中,全部創建自己的本地list<Weather> weather;

相反,嘗試爲你工作在全球list<Weather> weather。這是你用靜態局部(全球)天氣客體主代碼:

static list<Weather> weather; 

void displayFullForcast() 
{ 
    for (list<Weather>::iterator it = weather.begin(); it != weather.end(); ++it) 
    { 
     cout << *it << endl; 
    } 
} 

void displayTodayForcast() 
{ 
} 

void displayLowHigh() 
{ 
} 

void displayHumidity() 
{ 
} 


void displayMenu() 
{ 
    int choice = 0; 
    cout << "What option would you like to see:\n" 
     << "(1) Forcast for 5 days\n" 
     << "(2) Forcast for Today\n" 
     << "(3) The Lowest and Highest Temperature\n" 
     << "(4) Humidity for Today\n" << endl; 
    cin >> choice; 
    switch (choice) 
      { 
      case 1: displayFullForcast(); 
       break; 
      case 2: displayTodayForcast(); 
       break; 
      case 3: displayLowHigh(); 
       break; 
      case 4: displayHumidity(); 
       break; 
      } 
} 



void executeWeatherQuery() 
{ 
    http_client client(U("http://api.openweathermap.org")); 
    uri_builder builder(U("/data/2.5/forecast")); 
    builder.append_query(U("q"), U("Searcy,AR")); 
    builder.append_query(U("appid"), U("5ee41aef99a6e283abcdd5a04d89ae67")); 
    builder.append_query(U("units"), U("imperial")); 
    builder.append_query(U("mode"), U("json")); 

    http_response response = client.request(methods::GET, builder.to_string()).get(); 
    web::json::value forecastJson = response.extract_json(true).get(); 
    web::json::value forecastListJson = forecastJson.at(U("list")); 

    if (forecastListJson.is_array()) 
    { 
     for (size_t i = 0; i < forecastListJson.size(); i++) 
     { 
      web::json::value forecastDayJson = forecastListJson[i]; 
      web::json::value mainJson = forecastDayJson.at(U("main")); 
      web::json::value currentTemperatureJson = mainJson.at(U("temp")); 
      double currentTemperature = 0; 
      if (!currentTemperatureJson.is_null()) 
      { 
       currentTemperature = currentTemperatureJson.as_double(); 
      } 
      cout << "Current Temperature " << currentTemperature << endl; 

      web::json::value weatherJson = forecastDayJson.at(U("weather"))[0]; 
      web::json::value mainWeatherJson = weatherJson.at(U("main")); 
      string weatherDescription = ""; 
      if (!mainWeatherJson.is_null()) 
      { 
       weatherDescription = conversions::to_utf8string(mainWeatherJson.as_string()); 
      } 
      cout << "Weather Description " << conversions::to_utf8string(weatherDescription) << endl; 

      web::json::value MainJson = forecastDayJson.at(U("main")); 
      web::json::value minTemperatureJson = MainJson.at(U("temp")); 
      double minTemperature = 0; 
      if (!minTemperatureJson.is_null()) 
      { 
       minTemperature = minTemperatureJson.as_double(); 
      } 
      cout << "Min Temp: " << minTemperature << endl; 

      web::json::value MainMaxJson = forecastDayJson.at(U("main")); 
      web::json::value maxTemperatureJson = MainMaxJson.at(U("temp")); 
      double maxTemperature = 0; 
      if (!maxTemperatureJson.is_null()) 
      { 
       maxTemperature = maxTemperatureJson.as_double(); 
      } 
      cout << "Max " << maxTemperature << endl; 

      web::json::value MainHumidityJson = forecastDayJson.at(U("main")); 
      web::json::value humidityJson = MainHumidityJson.at(U("humidity")); 

      double humidity = 0; 
      if (!MainHumidityJson.is_null()) 
      { 
       humidity = humidityJson.as_double(); 
      } 
      cout << "Humidity: " << humidity << endl; 

      web::json::value MainTimeJson = forecastDayJson.at(U("dt_txt")); 
      string time = ""; 
      if (!MainTimeJson.is_null()) 
      { 
       time = conversions::to_utf8string(MainTimeJson.as_string()); 

       //time = localtime(time); 
      } 
      cout << "Time" << time << endl; 

      Weather WeatherForcast(currentTemperature, maxTemperature, minTemperature, 
       weatherDescription, humidity, time); 
      weather.push_back(WeatherForcast); 
     } 
    } 
} 

void main() 
{ 
    /*string location; 
    cout << "Please enter a city and a State (Example: Searcy,AR)" << endl; 
    cin >> location;*/ 
    executeWeatherQuery(); 
    displayMenu(); 
} 

注意如何只有一個weather多數民衆贊成操作上的文件範圍內的對象,而不是每個功能一個weather對象。

希望能有所幫助。

0

除了範圍界定問題,其他人所說的,你還需要編寫一個函數來打印對象:

std::ostream& operator <<(std::ostream& str, const Weather & w) 
{ 
    str << "Temp will be " << w.temp << std::endl; 
} 

您還需要使這個函數類的朋友。

(寫在手機上。編輯完成歡迎功能。)