S
Steven T. Hatton
Could someone explain this to me?
/*
* the headers and using declarations are the same for both examples
*/
#include <sstream>
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <string>
using std::stringstream;
using std:stream;
using std::make_pair;
using std::endl;
using std::vector;
using std:air;
using std::string;
/*
* This compiles. Note the pair<int, int>
*/
template <typename Tf, typename Ts>
ostream& print_pair_vector(const vector<pair<Tf,Ts> >& vec, ostream& out,
const string& name="v")
{
typedef vector<pair<int, int> >::const_iterator vpp_itr;
for(vpp_itr it = vec.begin(); it < vec.end(); it++) {
size_t i = it -vec.begin();
out << name << "["<< i << "].first= " << it->first
<< name << "["<< i << "].second=" << it->second
<< endl;
}
return out;
}
/*
* This won't compile. Note the pair<Tf, Ts> where <int, int> is above.
* The errors I get are shown below.
*/
template <typename Tf, typename Ts>
ostream& print_pair_vector(const vector<pair<Tf,Ts> >& vec, ostream& out,
const string& name="v")
{
typedef vector<pair<Tf, Ts> >::const_iterator vpp_itr;
for(vpp_itr it = vec.begin(); it < vec.end(); it++) {
size_t i = it -vec.begin();
out << name << "["<< i << "].first= " << it->first
<< name << "["<< i << "].second=" << it->second
<< endl;
}
return out;
}
/* error messages
g++ -o stacker Stacker.cpp main.cpp
Stacker.cpp: In function `std:stream& print_pair_vector(const
std::vector<std:air<_T1, _T2>, std::allocator<std:air<_T1, _T2> > >&,
std:stream&, const std::string&)':
Stacker.cpp:21: error: expected init-declarator before "vpp_itr"
Stacker.cpp:21: error: expected `,' or `;' before "vpp_itr"
Stacker.cpp:22: error: `vpp_itr' undeclared (first use this function)
Stacker.cpp:22: error: (Each undeclared identifier is reported only once for
each function it appears in.)
Stacker.cpp:22: error: expected `;' before "it"
Stacker.cpp:22: error: `it' undeclared (first use this function)
*/
/*
* the headers and using declarations are the same for both examples
*/
#include <sstream>
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <string>
using std::stringstream;
using std:stream;
using std::make_pair;
using std::endl;
using std::vector;
using std:air;
using std::string;
/*
* This compiles. Note the pair<int, int>
*/
template <typename Tf, typename Ts>
ostream& print_pair_vector(const vector<pair<Tf,Ts> >& vec, ostream& out,
const string& name="v")
{
typedef vector<pair<int, int> >::const_iterator vpp_itr;
for(vpp_itr it = vec.begin(); it < vec.end(); it++) {
size_t i = it -vec.begin();
out << name << "["<< i << "].first= " << it->first
<< name << "["<< i << "].second=" << it->second
<< endl;
}
return out;
}
/*
* This won't compile. Note the pair<Tf, Ts> where <int, int> is above.
* The errors I get are shown below.
*/
template <typename Tf, typename Ts>
ostream& print_pair_vector(const vector<pair<Tf,Ts> >& vec, ostream& out,
const string& name="v")
{
typedef vector<pair<Tf, Ts> >::const_iterator vpp_itr;
for(vpp_itr it = vec.begin(); it < vec.end(); it++) {
size_t i = it -vec.begin();
out << name << "["<< i << "].first= " << it->first
<< name << "["<< i << "].second=" << it->second
<< endl;
}
return out;
}
/* error messages
g++ -o stacker Stacker.cpp main.cpp
Stacker.cpp: In function `std:stream& print_pair_vector(const
std::vector<std:air<_T1, _T2>, std::allocator<std:air<_T1, _T2> > >&,
std:stream&, const std::string&)':
Stacker.cpp:21: error: expected init-declarator before "vpp_itr"
Stacker.cpp:21: error: expected `,' or `;' before "vpp_itr"
Stacker.cpp:22: error: `vpp_itr' undeclared (first use this function)
Stacker.cpp:22: error: (Each undeclared identifier is reported only once for
each function it appears in.)
Stacker.cpp:22: error: expected `;' before "it"
Stacker.cpp:22: error: `it' undeclared (first use this function)
*/