1

I have an unordered_map containing a struct I created called groups, with int keys and basically want to print the map as if it was a list but for some reason its giving an error.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <unordered_map>
#define UID "10014"

using namespace std;

typedef struct
{
    int num_users;
    char GNAME[26],GID[3];
    char UIDS[500][6];
}group;

std::unordered_map<int,group> groups;

int main()
{
    for (auto x : groups)
      cout << x.first << x.second endl;

   return(0);
}

The code gives me this error:

no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘group’)
       cout << x.first << " " << x.second << endl;
user4581301
  • 33,082
  • 7
  • 33
  • 54
Martim Correia
  • 483
  • 5
  • 16
  • When you define your own class you also need to define a `std::ostream& operator<<(std::ostream& os, const T& obj)` function (`T` here is `group`) that knows how to print out your class. [See here for more details](https://en.cppreference.com/w/cpp/language/operators#Stream_extraction_and_insertion) – user4581301 Dec 20 '21 at 22:04
  • 1
    Sidenote: You don't need to `typedef` `struct`s in C++. They are `typedef`ined automatically – Ted Lyngmo Dec 20 '21 at 22:09

0 Answers0