function as a function parameter

hi,
I have a function xref that takes another function as one of its parameters. however, I get the following error on compilation:
1
2
3

error: too few arguments to function ‘std::map<std::basic_string<char>, std::vector<int> > xref(std::istream&, std::vector<std::basic_string<char> > (*)(const std::string&))’


this is the relevant 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
map<string, vector<int> > xref(istream&, vector<string> (*find_words)(const string&));
vector<string> split(const string&);

int main()
{
  xref(cin);

   return 0;
}

map<string, vector<int> >xref(istream& in, vector<string> find_words(const string&)=split)
{
  string line;
  int line_num=0;
  map<string, vector<int> >ret;

  while(getline(in,line))
    {
      ++line_num;
      vector<string> words=find_words(line);

      for(vector<string>::const_iterator it=words.begin();it!=words.end();++it)
	{
	  ret[*it].push_back(line_num);
	}
    }
  return ret;
}
Put the default parameter in the declaration, not in the definition. The compiler performs a single pass, so by the time it finds that the function does in fact take default parameters, it has already reported the error.
thanks helios,
i did that, and removed the default parameter from the definition, but now I get the following error:
 
error: ‘split’ was not declared in this scope

now where do I move the declaration of split to?
Last edited on
Of course, you have to move the declaration of split() before the declaration of xref().
oops, that was my silly error...thx!
you are missing the pointer to your function parameter within your definition
 
map<string, vector<int> >xref(istream& in, vector<string> find_words(const string&))


Should Be
 
map<string, vector<int> >xref(istream& in, vector<string> (*find_words)(const string&))

thx jloundy,
i thought it was not necessary in the definition, but I'll fix it.
>>i thought it was not necessary in the definition,
I don't see why you could, but I usually typedef a function pointer for syntactical sugar
well, I ran it with this code in my main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  // xref(cin);

  typedef map<string,vector<int> > msvi;

   msvi rem=xref(cin);

  for (msvi::const_iterator iter=rem.begin();iter!=rem.end();++iter)
     cout<<iter->second.begin()<<endl;

   return 0;
}


and I am getting the following error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
error: no match foroperator<<’ in ‘std::cout << iter.std::_Rb_tree_const_iterator<_Tp>::operator-> [with _Tp = std::pair<const std::basic_string<char>, std::vector<int> >, const _Tp* = const std::pair<const std::basic_string<char>, std::vector<int> >*]()->std::pair<const std::basic_string<char>, std::vector<int> >::second.std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>, std::vector<_Tp, _Alloc>::const_iterator = __gnu_cxx::__normal_iterator<const int*, std::vector<int> >, typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::const_pointer = const int*]()’
/usr/include/c++/4.5/ostream:108:7: note: candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:117:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.5/ostream:127:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:165:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:169:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:173:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:91:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:180:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:105:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:191:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:200:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:204:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:209:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:213:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:221:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:225:7: note:                 std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:119:5: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]

The output stream cannot output iterators
 
cout<<iter->second.begin()<<endl;


I think you mean this

 
cout<<iter->second.at(0)<<endl;


yes indeed, that's what I meant.
thx jloundy!
Alternatively, you could use iter->second.front().
cool, thx helios!
so, I wanted to revise the code so it read from a file instead of cin, and I revised it to:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
  typedef map<string,vector<int> > msvi;
  string pfile="f2.txt";
    ifstream infile;
    infile.open(pfile.c_str());
   
 msvi rem=xref(infile);

      for (msvi::const_iterator iter=rem.begin();iter!=rem.end();++iter)
     {....}
return 0;
}

and the xref function to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
map<string, vector<int> >xref(ifstream& in , vector<string> (*find_words) (const string&))
{
  string line;
  int line_num=0;
  map<string, vector<int> >ret;

  //   std::string pfile="f2.txt";
  //std::ifstream infile;
  //infile.open(pfile.c_str());

  while(getline(in,line))
    {...}
return ret;
}


it compiles alright, but when i run the program, it still expects a cin input...
Topic archived. No new replies allowed.