Map with classes

Hello, I don't know how to output the map in class Running

code]
class Athlete
{

std::string name, country;
int year;

public:




Athlete();
Athlete(std::string _name, std::string _country, int _year);

std::string getName() const;
Athlete& setName(std::string _name);

std::string getCountry() const;
Athlete& setCountry(std:: string _country);

int getYear() const;
Athlete& setYear(int _year);

std::string toString() const;

bool operator<(const Athlete& second) const;
bool operator>(const Athlete& second) const;


friend std::ostream& operator<<(std::ostream& os, const Athlete& a);
friend std::istream& operator>>(std::istream& is, Athlete& a);


};
//that is cpp file
#include "Athlete.h"
#include<sstream>

Athlete::Athlete() :name("noname"), country("no"), year(0)
{

}

Athlete::Athlete(std::string _name,std::string _country, int _year)
{
name = _name;
country = _country;
year = _year;
}

std::string Athlete::getName() const
{
return name;
}

Athlete& Athlete::setName(std::string _name)
{
name = _name;
return *this;
}



std::string Athlete::getCountry() const
{
return country;
}

Athlete& Athlete::setCountry(std::string _country)
{
country = _country;
return *this;

}

int Athlete::getYear() const
{
return year;
}

Athlete& Athlete::setYear(int _year)
{
year = _year;
return *this;
}

std::string Athlete::toString() const
{
std::ostringstream os;
os.width(4);
os << name << " ";
os.width(4);
os <<country << " ";
os.width(4);
os << year << "g.";
return os.str();

}

bool Athlete::operator<(const Athlete& second) const
{
return name < second.name;
}

bool Athlete::operator>(const Athlete& second) const
{
return !(*this < second);
}

std::ostream& operator<<(std::ostream& os, const Athlete& a)
{
os << a.toString();
return os;
}

std::istream& operator>>(std::istream& is, Athlete& a)
{
is >> a.name >> a.country>>a.year;
return is;
}


[/code][
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<sstream>

class Time
{
    int hours;
	int minutes;

	int inMinutes() const;
	void correctTime();

public:
	Time();

	Time(int _hours, int _minutes);

	int getMinutes() const;
	int getHours() const;

	Time& setMinutes(int _min);
	Time& setHours(int _hours);


	std::string printTime() const;


	friend std::ostream& operator<<(std::ostream& os, const Time& t);
	friend std::istream& operator>>(std::istream& is, Time& t);
	

	Time operator-(const Time& second) const;

	bool operator<(const Time& second) const;
	bool operator>(const Time& second) const;

	bool operator<=(const Time& second) const;
	bool operator>=(const Time& second) const;

	bool operator==(const Time& second) const;
	bool operator!=(const Time& second) const;



};
// cpp file
#include "Time.h"
#include<iostream>

int Time::inMinutes() const
{
    return this->hours * 60 + this->minutes;
}

void Time::correctTime()
{
    if (minutes >= 60) {
		hours += minutes / 60;
		minutes = minutes % 60;
	}

}

Time::Time() :hours(0), minutes(0)
{

}

Time::Time(int _hours, int _min): hours(_hours), minutes(_min)
{
    correctTime();

}

int Time::getMinutes() const
{
    return minutes;
}

int Time::getHours() const
{
    return hours;
}

Time& Time::setMinutes(int _min)
{
    minutes = _min;
    correctTime();
    return *this;
}

Time& Time::setHours(int _hours)
{
    hours = _hours;
    return *this;
}


std::string Time::printTime() const
{
    std::ostringstream sout;
    if (hours < 10)
        std::cout << "\nTime: 0" << this->getHours();
    else
        std::cout << "\nTime: " << this->getHours();

    if (minutes < 10)
        std::cout << ":0" << this->getMinutes() << std::endl;
    else
        std::cout << ":" << this->getMinutes() << std::endl;
    return sout.str();
}


Time Time::operator-(const Time& second) const
{
    Time res;

    int difference = inMinutes() - second.inMinutes();
    if (difference > 0) {
        res = Time(0, difference);
    }
    else {
        res = Time(0, -difference);
        res.hours = -res.hours;
    }
    return res;

}


bool Time::operator<(const Time& second) const
{
    return inMinutes() < second.inMinutes();
}

bool Time::operator>(const Time& second) const
{
    return   !(*this < second);
}

bool Time::operator<=(const Time& second) const
{
    return  inMinutes() <= second.inMinutes();
}

bool Time::operator>=(const Time& second) const
{
    return   inMinutes() >= second.inMinutes();
}

bool Time::operator==(const Time& second) const
{
    return  inMinutes() <= second.inMinutes();
}

bool Time::operator!=(const Time& second) const
{
    return !(*this == second);
}

std::ostream& operator<<(std::ostream& os, const Time& t)
{
    os << t.printTime();
    return os;
}

std::istream& operator>>(std::istream& is, Time& t)
{
    is >> t.hours >> t.minutes;
    return is;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Athlete.h"
#include "Time.h"
#include <map>
#include <string>
#include <iostream>

class Running 
{
    
    int running_distance;
    std::map<Athlete, Time> participants_time;

public:


    Running();
    Running( int _run_dist,std::map<Athlete, Time>_participants_time);

    void addAthlete(Athlete a, Time t);

    std::string toString() const;

};
//cpp file
#include "Running.h"

Running::Running()
{

}


Running::Running( int _run_dist, std::map<Athlete, Time>_participants_time):
	running_distance(_run_dist),participants_time(_participants_time)
{


}

void Running::addAthlete(Athlete a, Time t)
{
	participants_time.insert(std::pair<Athlete, Time>(a, t));

}

std::string Running::toString() const
{
	for (const auto& pt : participants_time) {
		std::cout << pt.first << " e s vreme: " << pt.second << std::endl;
	}
}

#include<iostream>
#include "Time.h"
#include "Athlete.h"
#include "Running.h"

int main() {

	Athlete a{ "Vasil", "Belgia", 1982 };
	Time t{ 1, 20 };

	Running r{ 200,std::map<Athlete,Time> {a,t} };

}



I don't know how to output the map in class Running
1
2
3
4
5
6
std::string Running::toString() const
{
	for (const auto& pt : participants_time) {
		std::cout << pt.first << " e s vreme: " << pt.second << std::endl;
	}
}

Your Running seems to have member "toString()" that does output. Doesn't calling it do the deed?

However, that "toString()" promises to return a string but doesn't, and its name "to string" implies something else than shoveling anything into std::cout.
rather does not recognize me Running r{ 200,std::map<Athlete,Time> {a,t} }; Gives me an error at {a,t}, I do not know how to call it correctly
The problem is there is no std::map constructor that takes an Athlete and Time.
The following will compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include <utility>
//	#include "Time.h"
//	#include "Athlete.h"
//	#include "Running.h"

int main() {

	Athlete a{ "Vasil", "Belgia", 1982 };
	Time t{ 1, 20 };
	std::pair<Athlete, Time>	pr(a, t);
	std::map<Athlete, Time>		athmap;
	
	athmap.insert(pr);
	Running r( 200, athmap); 

}


p.s. Please edit your post and fix your opening code tag. You're missing the opening [
how to output the map

and
how to call it [constructor of std::map] correctly

Are two quite different problems.

Posting the verbatim error message that you get for
1
2
3
Athlete a{ "Vasil", "Belgia", 1982 };
Time t{ 1, 20 };
Running r{ 200,std::map<Athlete,Time> {a,t} };

in OP would draw our focus on the issue quicker.
Every time I load up the Beginner’s forum I read the title of this thread out of the corner of my eye as “Map with cheeses”.

And now I am trying to think of a good challenge thread using that title, and I am hungry.

LOL
Gouda, Swiss, Provolone? What type of cheesy comestible are you talking about?
I think my favorite is Dubliner, followed by Munster and Provolone and Swiss and any good, aged Cheddar (in no particular order). Bleu is also good.

I like taste. So, for example, a good cheesesteak will have both Swiss and Provolone on it.

A good grilled-cheese will have Munster and a little garlic salt.

Even American has its place, though. Works well on a properly-made burger.
Yeah, I do love some Muenster cheese. Never heard of Dubliner before.

I like Colby Jack and Mozzarella, usually in 2lb blocks. Take a thin slice and put it on a stoned wheat cracker. Great snack.
I like Colby Jack, but it is something of a filler IMO. I probably eat way too much of it.
Mozzarella!

You’ve made me hungrier...
I am under long standing doctor's orders to consume a certain amount of calcium products a day. A cup of yogurt, 4 oz of cheese, 8 oz. of milk, etc.

I am taking medication to prevent getting kidney stones again, it leaches calcium. So I have to increase my calcium intake beyond the recommended daily amount.

A fine balancing act, since stones (mostly) form from excess calcium.

The med is also used to treat high blood pressure.

Ah, just 'net searched for Dubliner cheese. A "sharp" cheese, like Cheddar.

No thanks, I really don't like that type of cheese. Mild is more my style.

I wonder if the OP is gonna bother to look back at this thread they started, that we hijacked blathering on about cheeses. ;Þ
its not spelled right but there is a cheese that is pronounced 'more beer' :) So you can honestly say you are under dr's orders to have more beer...
Not a fan of beer, unless it is "Gut Chermann" beer. American brew is piss wasser.

I am partial to distilled. Blended Whiskies, Spiced Rums or Tater Vodka. Cheap stuff that mixes well and I don't load up on a lot calories as I would with beer.

If'n it's vodka it's gotta be made from taters and made in Poland.

My current fav is Lvov, 1.75L.

http://www.aries-wineny.com/wp-content/uploads/2016/08/lvov-2.jpg
And when it comes to cheese I buy 2 pounds "family" sized chunks, the cheaper the better.

Spending $8-9 for an 8 day supply of my "dairy medication" is doable. Spending more makes my fixed income squeak in panic.

Cottage cheese and yogurt are in the rotation as well. Cottage cheese is the cheaper alternative. Light and fit reduced sugar yogurts are pricey, but overall are priced similar to the chunk cheeses.
https://kidneystones.uchicago.edu/how-to-eat-a-high-calcium-low-sodium-diet/
I'd at least ask a doctor how eating around 125g of cheese a day on average, even cottage cheese, makes sense in any sort of balancing act.
Topic archived. No new replies allowed.