【问题标题】:error C2678: binary '>' : no operator found which takes a left-hand operand错误 C2678:二进制“>”:未找到采用左操作数的运算符
【发布时间】:2014-01-08 20:26:51
【问题描述】:

我收到了这个恼人的错误 C2678: binary '>' : no operator found 接受左操作数。 我想使用 find_if 来查找除数和迭代器的返回值,以添加到向量中。 我会很感激任何想法。

头文件

#ifndef SMTH_H
#define SMTH_H


#include <vector>
#include <iostream>

    using namespace std;

    using std::vector;

    namespace Calculator{
        namespace Cal{

            typedef unsigned int Uint;
            typedef vector<Uint> TVint;
            typedef vector<Uint>::const_iterator TIterator;
    /..../

        class TestPrim : public Sir
            {
            protected:
                Uint _val;      
                double _stopVal;

            public:
                Uint testPrim;
                TestPrim(Uint val);
                TestPrim& operator ++();
                bool operator () (Uint divizor);
                operator Uint ();
                friend bool operator > (Uint val, const TestPrim &src);

            };
        }
    }
    #endif

Cpp 文件

#include <iomanip>
#include "smth.h"
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

namespace Calculator{
    namespace Cal{
/..../

class TestPrim;

        void Prime::CalculeazaValori(int index)
        {
            if(index > MAX_PRIME)
            {
                throw errorCheck();
            }

            if(index == 0)
            {
                _elemente.push_back(2);
                _elemente.push_back(3);
            }

            for (TestPrim testPrim = (_elemente.back() + 2); _elemente.size() < _count; ++testPrim)
            {

                TIterator it = find_if (_elemente.begin(), _elemente.end(), testPrim  );
                if (it > testPrim) //error on this line
                {
                    _elemente.push_back(testPrim);
                }
            }
        }



    }
}

【问题讨论】:

  • 从不在标题中放置 using 指令或声明。我不希望在包含标题时引入额外的垃圾。
  • @chris 我会记住的。谢谢!

标签: c++ visual-studio


【解决方案1】:

这是您的操作员的样子:

friend bool operator > (Uint val, const TestPrim &src);

这就是你使用它的方式:

it > testPrim

itTIterator,而不是 Uint。大概你的意思是

*it > testPrim

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多