Re: Print map values with class as map member
testcpp@gmail.com wrote:
#include <iostream>
using std::cout;
using std::endl;
using std::ostream;
#include<map>
using std::map;
Not related to your question, but this can be dangerous, since 'using'
issued before including a header affects that header (and any headers
that header include) which is not the intent. In this particular case
it's OK, but be warned.
[skipped]
/* Class M to be used as part of map. */
class M {
public:
M() {};
M(int, vector<int>) {int i = 0; vector <int> table2;};
Again, not related to your question, but you probably want the effect
of this code here:
M(int, vector< int > const&): i(0) { }
This prevents copying of vector< int > parameter by using a const
reference instead of passing by value. It also uses member
initialization instead of assignment in the body of constructor.
Moreover, your version does not assign any value to the class member
M::i-- it initializes auto variable 'i', which is probably not the
intent.
~M() {};
private:
int i;
vector<int> mv;
};
ostream& operator<<(ostream &output, const int &x, const map<int, M>
&y) {
output << x << endl << y;
return output;
};
This will not work, since operator<< is binary operator and must take
exactly two arguments-- not less, not more.
/* This will not work, a failed attempt.
ostream& operator<<(ostream &output, const &vector<int> mv ) {
output << x;
return output;
};
*/
Surely. This function will call itself, and again, and again :)
(supposing 'x' is mistyped for 'mv' here)
typedef map<int, M>::const_iterator con_it;
for( con_it x = map_test.begin(); x !=map_test.end(); ++x ) {
cout << x->first << endl << x->second << endl;
}
}
(not related again, but returning a value from the 'main' function can
be considered a good practice)
Here, 'x' is of type std::pair< int, M > (or eqivalent), so 'x->first'
is of type int and 'x->second' is of type M. Next, the line
cout << x->first << endl << x->second << endl;
can be interpreted as (ignoring the return values and assuming they are
std::cout):
operator<<(cout, x->first);
operator<<(cout, endl);
operator<<(cout, x->second); // here is the catch
operator<<(cout, endl);
Your program does not define a proper operator for the third line.
This operator should have the following signature:
std::ostream& operator<<(std::ostream& out, M const& m);
If I leave out this section:
typedef map<int, M>::const_iterator con_it;
for( con_it x = map_test.begin(); x !=map_test.end(); ++x ) {
cout << x->first << endl << x->second << endl;
The programme compiles and builds correctly. Once I add it in (without
the overloaded << operator), I have linker errors indicating that the
compiler cannot find an << operator that takes an int and a const "M."
As a work around, I've attempted to create an overloaded << operator to
accomodate my user defined map, but nothing seems to work correctly.
I've tried several things including:
1. Created two separate overloaded operators, one for the two parts of
the map: an int and a vector.
- There is already an overload for int type (and other built-in types)
in the standard library.
- Neither int nor vector can be considered the 'parts' of std::map<
int, M >. Here, int is a mapped type and class M is a mapped-to type.
The type 'vector' have nothing to do with std::map< int, M > construct.
2. Created an overloaded << operator with the following parameters:
ostream, int, ostream and vector. (as const and non-const references)
This will not work too (see above).
3. Created an overloaded << operator with the following parameters:
ostream, map.
This is basically not a bad place for your 'for' loop above, but does
not solve your very problem.
Am I attempting something not capable of working, that is using a class
as a member of a map? If I remove the class and just build a simple
map with two ints as members, I have no trouble. I've spent several
days on this but am not making much progress and would like to move on
to hash tables and learn those.
You have no trouble with std::map< int, int >, since the overload of
operator<< for x->first is fitting x->second as well.
Thanks.
Alex
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]