-1

I want to build a container for fractions. The key will represent the value of the fraction in double; The numerator and the denominator are stored in a pair.

std::map<double, pair<int, int>> m ();

I had problems with inserting and printing the elements. Can you tell me how to do it for this specific case?

I tried:

m.insert(make_pair(x/y, make_pair(x, y)));
gives me error

request for member ‘insert’ in ‘m’, which is of non-class type ‘std::map<double, std::pair<int, int> >()’ m.insert(make_pair(x/y, make_pair(x, y)));

for ( auto it1 = m.begin(); it1!=m.end(); ++it1) cout << it1->first << "->" << it1->second << endl; gives

request for member ‘begin’ in ‘m’, which is of non-class type ‘std::map<double, std::pair<int, int> >()’ for ( auto it1 = m.begin(); it1!=m.end(); ++it1)

and many other ways shown on geeksforgeeks and SO that gave me the same error but i don't know why.

Simona
  • 53
  • 1
  • 12
  • 2
    You're looking for the problem in the wrong place. `std::map> m (); ` declares a function `m` that takes no arguments and returns a `std::map>` - i.e. the function type that the compiler tells you that it has. Remove the `()`. (You can use `{}` instead if you like.) – molbdnilo Nov 01 '21 at 09:12

1 Answers1

0

Your problems lies on what molbdino says:

std::map<double, pair<int, int>>  m ();   

The compiler thinks that it's a function name m, has no argument, and returns a std::map<double, pair<int, int>> which is not what you want.

So, remove the ()

Another thing is x is an int, y is an int, so x / y is an integer division and will return an int. But your map wants a double.

So you should typecast it into an double

  • Initially it was `std::map>` but it gave me segmentation fault in other part ot the program where I'm transforming x from string format to int format using stringstream and a function that I created. I thought that the problem is that the map haven't been initialised, but when i replace `()` with ``{}`` i also get segmentation fault . Maybe the function has some problem, but I've used it many times and there were no problems – Simona Nov 01 '21 at 09:44
  • function: `void tokenize(string const& str, const char delim, vector& out) { stringstream ss(str); string s; while (getline(ss, s, delim)) { out.push_back(s); } } ` in main: ` string container; getline(cin, container); vector out; const char delim = ' '; tokenize(container, delim, out); std::map> m {}; for(int i = 0; i < N; i++) { stringstream geek(out[i]); int x = 0, y = 1; geek >> x; } – Simona Nov 01 '21 at 09:53
  • Program terminated with signal SIGSEGV, Segmentation fault. #0 std::__cxx11::basic_stringbuf, std::allocator >::basic_stringbuf (__mode=24, __str=..., this=0x7ffc91fd7748) at /usr/local/include/c++/8.3.0/ext/new_allocator.h:81 81 new_allocator(const new_allocator&) _GLIBCXX_USE_NOEXCEPT { } – Simona Nov 01 '21 at 09:58
  • @Simona You should ask a new question about the problem that you arise. – justANewb stands with Ukraine Nov 01 '21 at 13:13