【问题标题】:error C2679: binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)错误 C2679:二进制“>”:未找到采用“int”类型右侧操作数的运算符(或没有可接受的转换)
【发布时间】:2019-04-15 17:15:51
【问题描述】:

无论出于何种原因,当我尝试比较特定函子中的两个 int 值时,它只会给我这个错误

MVCE

首先是我称之为函子的部分

其次是类的主体

第三个是函子的主体

int main()
{
    int compare;
    std::vector<int> vectInt({ 1, 2, 11, 12, 34 });
    std::cout << "Enter value for comparing: ";
    std::cin >> compare;
    int count = countGreater<int>()(vectInt, compare);
    return 0;
}

class SquareTriangle
{
    int cathetus1, cathetus2;
public:
    SquareTriangle() {}
    ~SquareTriangle() {}
    SquareTriangle(int first, int second)
    {
        this->cathetus1 = first;
        this->cathetus2 = second;
    }
    double getArea() const
    {
        return (cathetus1 * cathetus2) / 2;
    }
    friend bool operator < (const SquareTriangle &first, const SquareTriangle &second)
    {
        if (first.getArea() < second.getArea())
            return true;
        else
            return false;
    }

    friend bool operator > (const SquareTriangle &first, const SquareTriangle &second)
    {
        if (first.getArea() > second.getArea())
            return true;
        else
            return false;
    }
    friend double operator << (const SquareTriangle &first, const SquareTriangle &second)
    {
        return first.getArea();
    }
    friend double operator += (const SquareTriangle &first, const SquareTriangle &second)
    {
        first.getArea() += second.getArea();
        return first.getArea();
    }
};
typedef SquareTriangle ST;

template <typename Q>
class countGreater
{
    int count = 0;
public:
    int operator () (const std::vector<Q> &ptr, int compare = 0)
    {
        if (sizeof(Q) == sizeof(ST))
        {
            int first, second;
            std::cout << "Enter first cathetus: ";
            std::cin >> first;
            std::cout << "Enter second cathetus: ";
            std::cin >> second;
            ST valueST(first, second);
            for (int i = 0; i < ptr.size(); i++)
            {
                if (ptr[i] > valueST)
                {
                    count++;
                }
            }
        }
        else
        {
            for (int i = 0; i < ptr.size(); i++)
            {
                *if (ptr[i] > compare)*
                {
                    count++;
                }
            }
        }   
        std::cout << "Number of elements greater than chosen: ";
        return count;
    }
};

给出错误的行

if (ptr[i] > compare)

完整的错误信息 C2679: 二进制 '>' : 未找到采用 'int' 类型右侧操作数的运算符(或没有可接受的转换)

【问题讨论】:

  • 你怎么称呼这个函子?你用什么Q?请提供minimal reproducible example
  • 1.不要将相关代码作为评论发布,edit 将其发布到问题中。 2. 那仍然不是minimal reproducible example,它缺少重要信息,例如ST 的定义和完整的错误信息
  • 您仍然拥有三个独立的代码块,而不是 MVCE。编写一个您认为应该自行编译的代码 sn-p。
  • @ildjarn 如果我又没有做错什么,那就是。我没有包括我使用的所有库,因为我认为它们没有发挥任何作用,但如果需要我可以编辑它们
  • 在将您的代码修复为实际的 MCVE (godbolt.org/z/g56o1W) 后,我在 if (ptr[i] &gt; valueST) 而不是 if (ptr[i] &gt; compare) 上收到错误

标签: c++ visual-studio


【解决方案1】:

如果我重新排序并修复#includes,您的代码中至少有两个错误。首先,这不会编译:

friend double operator += (const SquareTriangle &first, const SquareTriangle &second)
{
    first.getArea() += second.getArea();
    return first.getArea();
}

因为您正在尝试将某些内容分配给表达式。如果要增加first 的区域,则必须修改数据成员(catheti)。不知道你为什么要这样做,因为无论如何它没有多大意义。


其次,这个if 行不编译:

if (ptr[i] > valueST)
{
    count++;
}

因为ptr[i] 最终是一个整数,而valueST 是你的类的一个实例。由于您无法将intSquareTriangle 进行比较,因此它会中断。不过,不确定您要做什么。与该地区相比,也许?

【讨论】:

  • @UnholySheep 关于第一个错误,您可以忽略它,因为我正在处理代码的其他一些片段并且它在 MCVE 中没有任何作用,至于第二个我理解您在尝试什么说,如果我使用标准整数而不是我的 ST 类,我有什么办法可以告诉程序忽略第一个 if if (ptr[i] &gt; valueST)
  • 有一些方法可以根据编译时的条件编译不同的代码,但首先你真的需要了解你想要做什么,这并不清楚。如果我是你,我会提出一个新问题,试图明确你想要达到的目标。希望有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多