Re: operator+ on nested type within template class
Michael Kiermaier ha scritto:
Hello,
I have this code:
#include <iostream>
template<unsigned int modulus> class modRing {
public:
class element {
public:
element(unsigned int valIn):
val(valIn)
{}
unsigned int val;
};
};
template<unsigned int modulus>
typename modRing<modulus>::element
operator+(typename modRing<modulus>::element const &a, typename
modRing<modulus>::element const &b) {
return typename modRing<modulus>::element((a.val + b.val) % modulus);
}
int main() {
typedef modRing<5> R;
R::element a(3);
R::element b(4);
std::cout << (a + b).val;
}
Compilation with g++ 4.1.2 results in:
In function ???int main()???:
24: error: no match for ???operator+??? in ???a + b???
So the compiler does not recognize that it can specialize the operator+
function to the situation in line 24. My questions are:
Why?
"typename modRing<modulus>::element" is a so-called "non-deduced"
context that doesn't allow the compiler to deduce modulus.
How can I fix this problem?
template<unsigned int modulus> class modRing {
public:
class element {
public:
element(unsigned int valIn):
val(valIn)
{}
unsigned int val;
friend element operator+(element const &a,
element const &b) {
return element((a.val + b.val) % modulus);
}
};
};
HTH,
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]