I get a strange compiler error from g ++.
It says " std::plus is not a function
" for the following code, although I do not include <functional>
, and I do not use std
where the error occurs.
Here is the code:
#include <iostream> template<class T1, class T2, class T3> struct MyStruct { T1 t1; T2 t2; T3 t3; MyStruct() {} MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_) : t1(t1_), t2(t2_), t3(t3_) {} }; template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { // ... } int main() { typedef MyStruct<int, double, std::string> Struct; Struct x(2, 5.6, "bar"); Struct y(6, 4.1, "foo"); Struct result = plus(x, y); }
Here is the complete error (slightly reformatted):
/usr/include/c++/4.2.1/bits/stl_function.h: In function 'int main()': /usr/include/c++/4.2.1/bits/stl_function.h:134: error: 'template<class _Tp> struct std::plus' is not a function, plus3.cc:13: error: conflict with 'template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(const MyStruct<T1, T2, T3>&, const MyStruct<T1, T2, T3>&)' plus3.cc:21: error: in call to 'plus'
Does anyone know why this is so and how to avoid a mistake? I would really like to call the plus
function.
The error does not occur if my plus
function does not have three template arguments, which makes sense after defining the definition of std::plus
:
template <class _Tp> struct plus : public binary_function<_Tp, _Tp, _Tp>
But it is still strange, because std::plus
should not even be known at this point.
UPDATE:
In response to some answers, I insert a slightly modified code that also gives an error. My plus
function is in namespace foo
here, and it is called from the same namespace, so there is no need to qualify it with foo::
#include <string> namespace foo { template<class T1, class T2, class T3> struct MyStruct { T1 t1; T2 t2; T3 t3; MyStruct() {} MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_) : t1(t1_), t2(t2_), t3(t3_) {} }; template<class T1, class T2, class T3> MyStruct<T1, T2, T3> plus(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { // ... } template<class T1, class T2, class T3> MyStruct<T1, T2, T3> bar(MyStruct<T1, T2, T3> const& x, MyStruct<T1, T2, T3> const& y) { return plus(x, y); } } // end foo int main() { typedef foo::MyStruct<int, double, std::string> Struct; Struct x(2, 5.6, "bar"); Struct y(6, 4.1, "foo"); Struct result = foo::bar(x, y); }