so I have a Date Class, and each Date object stores a day, month, and year variable for the object. I also have a data file with multiple dates, many duplicate dates, and all with their own value for a float.
I am trying to store all my dates into a multimap as the key, so i can have all my duplicate dates with their floats in there too. My goal, is that I want a user to be able to enter a month and a year, and then I search my map for values with that month and year in the date object, and return the speeds for those objects. However, I get this error:
error: no matching function for call to 'make_pair(Date&, float)'|
for this declaration
mapOption1.insert(make_pair<Date, float>(windlog[x].d, windlog[x].speed.GetSpeed()));
I don't understand why, as when I use windlog[x].d (where windlog is a vector of a struct, and d is the date object it holds at position x) in Binary Search Tree, I did not need a Get Method to store the objects in the BST. Why would I need it now? Assuming that is the issue.
Here is the code with my problem:
multimap<Date, float> mapOption1;
for(int x = 0; x < SIZE; x++)
{
mapOption1.insert(make_pair<Date, float>(windlog[x].d, windlog[x].speed.GetSpeed()));
}
Minimal reproducible example:
multimap<Date, float> mapOption1;
Date datetest;
datetest.SetDay("20");
datetest.SetMonth("3");
datetest.SetYear("2014");
for(int x = 0; x < SIZE; x++)
{
mapOption1.insert(make_pair<Date, float>(datetest, 47.5);
}
Date.h:
#if !defined(_DATE_H)
#define _DATE_H
#include <iostream>
#include <string>
#include <tuple>
using namespace std;
/**
* @class Date
* @brief Manages and holds all dates
*
* This class is used to convert string from the input file into numerical values
* and then store them as date values
* @author Darren Fernando
*
* @date 05/05/2020 Darren Fernando, Started
* @bug No bugs founds as of yet
*/
class Date
{
public:
Date();
Date(unsigned day1, unsigned month1, unsigned year1);
/**
* @brief A set method that is used to set the day value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable day
* @param day1 - A string variable that is converted
* @return void
*/
void SetDay(string day1);
/**
* @brief A set method that is used to set the month value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable month
* @param month1 - A string variable that is converted
* @return void
*/
void SetMonth(string month1);
/**
* @brief A set method that is used to set the year value
* It takes a string parameter and calls another method to convert it, and then assign it to the variable year
* @param year - A string variable that is converted
* @return void
*/
void SetYear(string year1);
/**
* @brief A get method that is used to get the day value by returning it
* @return unsigned
*/
unsigned GetDay() const;
/**
* @brief A get method that is used to get the month value by returning it
* @return unsigned
*/
unsigned GetMonth() const;
/**
* @brief A get method that is used to get the year value by returning it
* @return unsigned
*/
unsigned GetYear() const;
/**
* @brief A method that converts a string to an unsigned numerical value
* @param time - A string parameter that is converted
* @return unsigned
*/
unsigned convertString(string date) const;
/**
* @brief A method that takes an input stream and perfoms some operations to set the date from this stream
* This method isn't used but may be valuable later on
* @param input - An input stream
* @return void
*/
void SetDate(istream &input);
/**
* @brief A method that takes an output stream and outputs it
* This method isn't used but may be valuable later on
* @param os - An output stream
* @return void
*/
void GetDate(ostream & os) const;
unsigned GetDateRaw() const;
private:
int day; /// Variable to store the day
int month; /// Variable to store the month
int year; /// Variable to store the year
};
bool operator==(const Date& firstDate, const Date& secondDate);
bool operator>(const Date& firstDate, const Date& secondDate);
bool operator!=(const Date& firstDate, const Date& secondDate);
ostream & operator <<(ostream & os, const Date & D); /// Operator << overload
istream & operator >>(istream & input, Date & D); /// Operator >> overload
#endif //_DATE_H
Date.cpp:
//
//
// Generated by StarUML(tm) C++ Add-In
#include "Date.h"
// Default constructor
Date::Date(){}
/* Date constructor to initialize an object of date with the passed parameters
*/
Date::Date(unsigned day1, unsigned month1, unsigned year1) {
day = day1;
month = month1;
year = year1;
}
void Date::SetDay(string day1) {
day = convertString(day1); // Calls method convertString to convert the string parameter and assign the new value to day.
}
void Date::SetMonth(string month1) {
month = convertString(month1); // Calls method convertString to convert the string parameter and assign the new value to month.
}
void Date::SetYear(string year1) {
year = convertString(year1); // Calls method convertString to convert the string parameter and assign the new value to year.
}
unsigned Date::GetDay() const {
return day; // Returns the day of the object
}
unsigned Date::GetMonth() const {
return month; // Returns the month of the object
}
unsigned Date::GetYear() const {
return year; // Returns the year of the object
}
/* This method is used for directly setting the date from a file. It is not used now,
but may prove handy in the future.*/
void Date::SetDate(istream &input){
string day1;
string month1;
string year1;
getline(input, day1, '/'); //Reads line until '/' is found and stores the string in day1
getline(input, month1, '/'); //Reads line until '/' is found and stores the string in month1
getline(input, year1, ' '); //Reads line until ' ' is found and stores the string in year1
/*Sets the strings with setters which convert them automatically
*/
SetDay(day1);
SetMonth(month1);
SetYear(year1);
}
//This method is designed to convert a string to an integer
inline unsigned Date::convertString(string date) const{
int date2 = 0;
date2 = stoi(date); //Uses stoi from cmath library to convert a string to an integer
return date2;
}
// A get function that uses the overloaded output stream to output the date in a specific format.
void Date::GetDate(ostream &os) const{
os << GetDay() << "/" << GetMonth() << "/" << GetYear() << " ";
}
unsigned Date::GetDateRaw() const{
return month && year;
}
bool operator==(const Date& firstDate, const Date& secondDate)
{
if(firstDate.GetYear() == secondDate.GetYear() && firstDate.GetMonth() == secondDate.GetMonth() && firstDate.GetDay() == secondDate.GetDay())
{
return true;
}
else
{
return false;
}
}
/*bool operator!=(const Date& firstDate, const Date& secondDate)
{
if(firstDate!=secondDate)
{
return true;
}
return false;
}*/
bool operator>(const Date& firstDate, const Date& secondDate)
{
if(firstDate.GetYear() > secondDate.GetYear())
{
return true;
}
if(firstDate.GetYear() == secondDate.GetYear())
{
if(firstDate.GetMonth() > secondDate.GetMonth())
{
return true;
}
else if(firstDate.GetMonth() == secondDate.GetMonth())
{
if(firstDate.GetDay() > secondDate.GetDay())
{
return true;
}
}
}
return false;
}
// Input stream operator overload
istream & operator >>( istream & input, Date & D) {
D.SetDate(input);
return input;
}
// Output stream operator overload
ostream & operator <<( ostream & os, const Date & D) {
D.GetDate(os);
return os;
}