S
Steven T. Hatton
Suppose I overload an operator in namespace A. How can I introduce it into
another namespace B? The following fails with the error shown
subsequently:
#include <iostream>
#include <functional>
#include <boost/array.hpp>
namespace A {
namespace {
using boost::array;
}
template<typename T, size_t Size_S>
array<T,Size_S>& operator+=(array<T,Size_S>& lhs, const array<T, Size_S>&
rhs)
{
std::transform(lhs.begin(), lhs.end()
, rhs.begin()
, lhs.begin()
, std:lus<T>());
return lhs;
}
}
namespace B {
namespace {
using boost::array;
using A:perator+=(array<T,Size_S>& lhs, const array<T, Size_S>& rhs);
}
void f() {
array<double, 4> a = {1,2,3,4};
array<double, 4> b = {5,6,7,8};
a+=b;
copy(a.begin(),a.end(),std:stream_iterator<double>(std::cout," "));
std::cout << std::endl;
}
}
int main(){
B::f();
}
-*- mode: compilation; default-directory: "~/code/c++/scratch/stl-ops/" -*-
g++ -oopassign opassign.cc -I$CPPSTH -I$BOOST_INCLUDE
opassign.cc:22: error: syntax error before `array'
opassign.cc: In function `void B::f()':
opassign.cc:28: error: no match for 'operator+=' in 'a += b'
distcc[2235] ERROR: compile on localhost failed
Compilation exited abnormally with code 1 at Tue Nov 9 23:07:37
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell
another namespace B? The following fails with the error shown
subsequently:
#include <iostream>
#include <functional>
#include <boost/array.hpp>
namespace A {
namespace {
using boost::array;
}
template<typename T, size_t Size_S>
array<T,Size_S>& operator+=(array<T,Size_S>& lhs, const array<T, Size_S>&
rhs)
{
std::transform(lhs.begin(), lhs.end()
, rhs.begin()
, lhs.begin()
, std:lus<T>());
return lhs;
}
}
namespace B {
namespace {
using boost::array;
using A:perator+=(array<T,Size_S>& lhs, const array<T, Size_S>& rhs);
}
void f() {
array<double, 4> a = {1,2,3,4};
array<double, 4> b = {5,6,7,8};
a+=b;
copy(a.begin(),a.end(),std:stream_iterator<double>(std::cout," "));
std::cout << std::endl;
}
}
int main(){
B::f();
}
-*- mode: compilation; default-directory: "~/code/c++/scratch/stl-ops/" -*-
g++ -oopassign opassign.cc -I$CPPSTH -I$BOOST_INCLUDE
opassign.cc:22: error: syntax error before `array'
opassign.cc: In function `void B::f()':
opassign.cc:28: error: no match for 'operator+=' in 'a += b'
distcc[2235] ERROR: compile on localhost failed
Compilation exited abnormally with code 1 at Tue Nov 9 23:07:37
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell