I'm getting a strange compiler error from g++.
It says "std::plus is not a function
" for the following code, even though I'm not including <functional>
and I'm not using 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 full 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 and how to avoid the error? I'd really like to call the function plus
.
The error doesn't occur when my plus
function doesn't have 3 template arguments, which makes some sense after looking at the definition of std::plus
:
template <class _Tp>
struct plus : public binary_function<_Tp, _Tp, _Tp>
But it's still strange because std::plus
shouldn't even be known at that point.
UPDATE:
In response to some answers, I'll paste slightly modified code which also gives the error. My plus
function is in namespace foo
here, and it is called from the same namespace, so there shouldn't be any need to qualify it using 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);
}