-1

What is wrong in this code as I am getting segmentation fault It seems returning map by value is not problematic.

#include<bits/stdc++.h>
using namespace std;

template<typename T>
T myMapIntersect(T& left, T& right)
{ }

int main()
{
    map<string, int> m;
    m["aa"] = 1;
    m["bb"] = 1;
    m["vf"] = 1;
    m["hh"] = 1;

    myMapIntersect< map<string,int> >(m, m);
    return 0;
}
username_4567
  • 4,737
  • 12
  • 56
  • 92
  • OT: you probably shouldn't include directly stuff from `bits`. – Ami Tavory Sep 28 '16 at 06:36
  • @AmiTavory Why? – username_4567 Sep 28 '16 at 06:36
  • 5
    um, where's the return in `myMapIntersect`? – kmdreko Sep 28 '16 at 06:38
  • Because it's a convention for things that are not guaranteed to remain fixed. Hypothetically, in the next version, the file will be `bits/std_cpp.hpp`. The outer directory files would modify accordingly, and they are the ones you should directly use. – Ami Tavory Sep 28 '16 at 06:38
  • See [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Bo Persson Sep 28 '16 at 06:40
  • It completely separates you from learning something you *should learn* : namely what constructs are provided by what headers from the standard library. Including a half-million lines of headers when all you need is `` and `` isn't instructive at all. – WhozCraig Sep 28 '16 at 06:40

1 Answers1

2

You need to return in your function

#include<bits/stdc++.h>
using namespace std;

template<typename T>
T myMapIntersect(T& left, T& right)
{
    return left;
}

int main()
{
    map<string, int> m;
    m["aa"] = 1;
    m["bb"] = 1;
    m["vf"] = 1;
    m["hh"] = 1;

    myMapIntersect< map<string,int> >(m, m);
    return 0;
}
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34