c++ - call of overloaded 'swap(char&, char&)' is ambiguous -
i can't figure out ambiguous swap(arr[i++],arr[n--]);
below. please educate me on wrongful ways.
#include <iostream> #include <string> template <typename t> void swap ( t & a, t & b ) { t temp = b; b = a; = temp; } template <typename t> void reverse_array ( t * arr, size_t n ) { size_t = 0; while (i < n) swap(arr[i++],arr[n--]); // problem line } int main () { char mystr [] = "obama smokes"; reverse_array(mystr, sizeof(mystr)/sizeof(char)); return 0; }
codepad has implicit using namespace std;
, bad idea , makes swap
conflict std::swap
. can use ::swap
instead, not before fixing bounds problem passing sizeof mystr - 2
instead of sizeof(mystr)/sizeof(char)
.
Comments
Post a Comment