a question in NS c++ programming
I added a new patch to my NS and I've seen these two errors. Does anyone know what I can do?
error: specialization of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = _AlgorithmTime]' in different namespace
from definition of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = _AlgorithmTime]'
and the errors are from this code
typedef struct _AlgorithmTime {
// Round.
int round;
// Fase.
int fase;
// Valore massimo di fase.
int last_fase;
public:
_AlgorithmTime() {
round = 0;
fase = 0;
last_fase = 0;
}
// Costruttore.
_AlgorithmTime(int r, int f, int l) {
round = r;
fase = f;
last_fase = l;
}
// Costruttore.
_AlgorithmTime(const _AlgorithmTime & t) {
round = t.round;
fase = t.fase;
last_fase = t.last_fase;
}
// Operatore di uguaglianza.
开发者_如何学编程 bool operator== (struct _AlgorithmTime & t) {
return ((t.fase == fase) && (t.round == round));
}
// Operatore minore.
bool operator < (struct _AlgorithmTime & t) {
if (round < t.round)
return true;
if (round > t.round)
return false;
if (fase < t.fase)
return true;
return false;
}
// Operatore maggiore.
bool operator > (struct _AlgorithmTime & t) {
if (round > t.round)
return true;
if (round < t.round)
return false;
if (fase > t.fase)
return true;
return false;
}
void operator++ () {
if (fase == last_fase) {
round++;
fase = 0;
return;
}
fase++;
}
void operator-- () {
if (fase == 0) {
round--;
fase = last_fase;
return;
}
fase--;
}
}AlgorithmTime;
template<>
bool
std::less<AlgorithmTime>::operator()(const AlgorithmTime & t1, const AlgorithmTime & t2)const
{
if (t1.round < t2.round)
return true;
if (t1.round > t2.round)
return false;
if (t1.fase < t2.fase)
return true;
return false;
}
Thanks
If your specialization of std::less<AlgorithmTime>::operator()
is inside your namespace, that's probably what the compiler is complaining about - you'd need to have the specialization at the 'global' namespace level.
As an aside, while it's fine to specialize std::less
, there's no need to in your case - the standard definition of std::less<AlgorithmTime>
will use the AlgorithmTime::operator<()
to do its work.
You could just specify the namespace of the template you are trying to specialize:
namespace std
{
template<>
bool
std::less<AlgorithmTime>::operator()(const AlgorithmTime & t1, const AlgorithmTime & t2)const
{
...
}
}
But as Michael Burr points out, there's no reason to specialize std::less
, if all you intend to do is duplicate the code of AlgorithmTime::<
.
The problem is that you are creating a template specialization of std::less::operator() for the AlgorithmTime class, but this specialization is in a different namespace than the original template of the function.
You can fix this by explicitly declaring your specialization to be in the std namespace, like so:
namespace std{
template<>
bool
less<AlgorithmTime>::operator()(const AlgorithmTime & t1, const AlgorithmTime & t2)const // "std::less" is no longer necessary
{
if (t1.round < t2.round)
return true;
if (t1.round > t2.round)
return false;
if (t1.fase < t2.fase)
return true;
return false;
}
}
精彩评论