c++ - NS c++编程中的一个问题

标签 c++

我为我的 NS 添加了一个新补丁,我看到了这两个错误。有谁知道我能做什么?

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]'

错误来自这段代码

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;
}

谢谢

最佳答案

如果你的专业std::less<AlgorithmTime>::operator()在您的命名空间内,这可能是编译器所提示的 - 您需要在“全局”命名空间级别进行专门化。

顺便说一句,虽然特化很好std::less ,在你的情况下没有必要 - std::less<AlgorithmTime> 的标准定义将使用 AlgorithmTime::operator<()完成它的工作。

关于c++ - NS c++编程中的一个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3033058/

相关文章:

c++ - sql参数的最佳STL容器

c++ - 在指针中使用 **

c++ - sizeof(int) 和 sizeof(int*) 有什么区别?此语句 int* numbers[] = {....} 是否正确?

c++ - 创建一个类似 Q_PROPERTY 的宏并提取 __VA_ARGS__

c++ - 有没有可以编译C++或C的库

c++ - vector 中 ‘.’ 标记之前的预期不合格 ID

c++ - 'objects' 的 Qt 所有权(T* 和 const T*)

c++ - 复制字符串集的正确方法是什么(在复制构造函数和赋值运算符中)?

c++ - 在 Ubuntu : build error on libgc 上构建 Virtuoso(使用 Mono 集成)

c++ - SCDynamicStoreContext 的函数指针字段有什么用?