遍历 objects 的列表
Looping through a list of objects
我试图遍历包含 object 的列表。我正在使用天气 API 读取数据并将每个数据成员存储在 object 中,然后将每个 object 存储在列表中。存储整个列表后,我希望能够遍历列表并显示 objects。我正在以 Json 的形式读取数据,并且我正在使用 casablanca 包来这样做。我想要做的就是能够在 main.cpp 的 displayFullForcast 函数中循环遍历我的列表。
这是我的列表定义:
list<Weather> weather;
这是我的 header:
#pragma once
#include<string>
#include<iostream>
#include <list>
using std::list;
using std::string;
//const static int MAX_DAYS = 5;
class Weather
{
public:
Weather();
~Weather();
Weather(double currentTemperature, double maxTemperature,
double minTemperature, string weatherDescription,double humidity, string time);
list<Weather> weather;
private:
string cityState;
double currentTemperature;
double highTemperature;
double lowTemperature;
string weatherDescription;
double humidity;
double highestTemperature;
double lowestTemperature;
string time;
};
这是我的实现:
#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();
}
** 我仍然在 cout << *it << endl 线上收到此错误;
错误:没有运算符“<<”匹配这些操作数。操作数类型是 std::ofstream 和天气。
您的问题似乎是在您的每个函数中都有一个单独的本地天气列表。
因此您将数据收集到一个列表中,该列表在函数结束时被破坏,然后显示刚刚创建的另一个列表的内容(并且是空的)。
取而代之的是一个全局列表天气。
我没有安装特定天气 API 包,但从您的代码来看,您似乎只是遇到了范围界定问题。
在您的 executeWeatherQuery
中,您声明一个 list<Weather> weather;
然后执行 weather.push_back(WeatherForcast);
;该 weather
对象仅对 executeWeatherQuery
函数而言是本地的,因此一旦该函数结束,该 weather
对象就会超出范围并且其中的数据将不再有效。此外,您的其他功能 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
对象。
希望能帮到你。
除了其他人提到的作用域问题,您还需要编写一个函数来打印对象:
std::ostream& operator <<(std::ostream& str, const Weather & w)
{
str << "Temp will be " << w.temp << std::endl;
}
您还需要将此函数设为 class 的好友。
(写在phone上。欢迎编辑完成功能。)
我试图遍历包含 object 的列表。我正在使用天气 API 读取数据并将每个数据成员存储在 object 中,然后将每个 object 存储在列表中。存储整个列表后,我希望能够遍历列表并显示 objects。我正在以 Json 的形式读取数据,并且我正在使用 casablanca 包来这样做。我想要做的就是能够在 main.cpp 的 displayFullForcast 函数中循环遍历我的列表。 这是我的列表定义:
list<Weather> weather;
这是我的 header:
#pragma once
#include<string>
#include<iostream>
#include <list>
using std::list;
using std::string;
//const static int MAX_DAYS = 5;
class Weather
{
public:
Weather();
~Weather();
Weather(double currentTemperature, double maxTemperature,
double minTemperature, string weatherDescription,double humidity, string time);
list<Weather> weather;
private:
string cityState;
double currentTemperature;
double highTemperature;
double lowTemperature;
string weatherDescription;
double humidity;
double highestTemperature;
double lowestTemperature;
string time;
};
这是我的实现:
#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();
}
** 我仍然在 cout << *it << endl 线上收到此错误; 错误:没有运算符“<<”匹配这些操作数。操作数类型是 std::ofstream 和天气。
您的问题似乎是在您的每个函数中都有一个单独的本地天气列表。
因此您将数据收集到一个列表中,该列表在函数结束时被破坏,然后显示刚刚创建的另一个列表的内容(并且是空的)。
取而代之的是一个全局列表天气。
我没有安装特定天气 API 包,但从您的代码来看,您似乎只是遇到了范围界定问题。
在您的 executeWeatherQuery
中,您声明一个 list<Weather> weather;
然后执行 weather.push_back(WeatherForcast);
;该 weather
对象仅对 executeWeatherQuery
函数而言是本地的,因此一旦该函数结束,该 weather
对象就会超出范围并且其中的数据将不再有效。此外,您的其他功能 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
对象。
希望能帮到你。
除了其他人提到的作用域问题,您还需要编写一个函数来打印对象:
std::ostream& operator <<(std::ostream& str, const Weather & w)
{
str << "Temp will be " << w.temp << std::endl;
}
您还需要将此函数设为 class 的好友。
(写在phone上。欢迎编辑完成功能。)