简体   繁体   English

二进制表达式的无效操作数(“ int_node”和const“ int_node”)

[英]invalid operands to binary expression ('int_node' and const 'int_node')

I'm a C++ beginner, many questions around me. 我是C ++初学者,周围有很多问题。 I have defined != operator of int_node , but when I compile this code, show error: 我已经定义了int_node !=运算符,但是当我编译这段代码时,显示错误:

invalid operands to binary expression ('int_node' and const 'int_node') 二进制表达式的无效操作数(“ int_node”和const“ int_node”)

The IDE that I use is xcode 4.6. 我使用的IDE是xcode 4.6。

Below is my all code 下面是我的所有代码

typedef struct int_node{
    int val;
    struct int_node *next;
} int_t;

template <typename Node>
struct node_wrap{
    Node *ptr;

    node_wrap(Node *p = 0) : ptr(p){}
    Node &operator *() const {return *ptr;}
    Node *operator->() const {return ptr;}

    node_wrap &operator++() {ptr = ptr->next; return *this;}
    node_wrap operator++(int) {node_wrap tmp = *this; ++*this; return tmp;}

    bool operator == (const node_wrap &i) const {return ptr == i.ptr;}
    bool operator != (const node_wrap &i) const {return ptr != i.ptr;}
} ;

template <typename Iterator, typename T>
Iterator find(Iterator first, Iterator last,  const T& value)
{
    while (first != last && *first != value) // invalid operands to binary experssion ('int_node' and const 'int_node')
    {
        ++first;
        return first;
    }
}

int main(int argc, const char * argv[])
{
    struct int_node *list_head = nullptr;
    struct int_node *list_foot = nullptr;
    struct int_node valf;
    valf.val = 0;
    valf.next = nullptr;
    find(node_wrap<int_node>(list_head), node_wrap<int_node>(list_foot), valf);

    return (0);
}

My compiler says 我的编译器说

"1>main.cpp(28): error C2676: binary '!=' : 'int_node' does not define this operator or a conversion to a type acceptable to the predefined operator" “ 1> main.cpp(28):错误C2676:二进制'!=': 'int_node'未定义此运算符或未转换为预定义运算符可接受的类型”

which is true. 没错

We could define != in terms of == eg for your missing int_node 我们可以用==来定义!= ,例如为您缺少的int_node

bool operator == (const int_node &i) const {return val == i.val;}
bool operator != (const int_node &i) const {return !(*this==i);}

You need to define the operators - should they check the node as well? 您需要定义运算符-他们是否也应该检查节点?

BTW, do you intend to return first no matter what? 顺便说一句,你打算回first不管是什么?

while (first != last && *first != value)
{
    ++first;
    return first;
    //     ^^^^^
    //     |||||
}

I see two things in there: 我在那里看到两件事:

  • struct int_node does not define any comparison operator between itself and a const int_node & instance, and that responds to your question; struct int_node本身与const int_node &实例之间未定义任何比较运算符,它可以回答您的问题;
  • in the function Iterator find(Iterator first, Iterator last, const T& value) there is a return statement within the while statement, so the while is useless, or the return should be put outside it. Iterator find(Iterator first, Iterator last, const T& value)函数Iterator find(Iterator first, Iterator last, const T& value)中, while语句内有一个return语句,因此while没用,否则应将return值放在它的外面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二进制表达式(&#39;std :: ostream&#39;(aka&#39;basic_ostream <char> &#39;)和&#39;const std :: vector <int> “) - invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'const std::vector<int>') 错误:类型为&#39;const char [3]&#39;和&#39;int *&#39;的无效操作数为二进制&#39;operator *&#39; - Error: invalid operands of types 'const char [3]' and 'int*' to binary 'operator*' “错误:二进制&#39;operator &lt;&lt;&#39;|的类型为&#39;int&#39;和&#39;const char [2]&#39;的无效操作数” - “error: invalid operands of types 'int' and 'const char [2]' to binary 'operator<<'|” C ++重载&gt;对于priority_queue(最小堆):在push()上为堆生成“对二进制表达式无效的操作数(&#39;const Node&#39;和&#39;const Node&#39;)” - C++ overloading > for priority_queue (min heap): yields “invalid operands to binary expression ('const Node' and 'const Node')” on push() for heap 类型为&#39;int&#39;和&#39;const char [15]&#39;的无效操作数为二进制&#39;operator &lt;&lt;&#39;^ - invalid operands of types ‘int’ and ‘const char [15]’ to binary ‘operator<<’ ^ 'int' 和 'const char [11]' 类型的无效操作数到二进制 'operator&lt;&lt;' - invalid operands of types 'int' and 'const char [11]' to binary 'operator<<' 'int' 和 'int [3]' 类型的无效操作数到二进制 'operator*' - invalid operands of types ‘int’ and ‘int [3]’ to binary ‘operator*’ 二进制表达式的无效操作数(&#39;RadioDevice&#39;和&#39;const RadioDevice&#39;) - Invalid operands to binary expression ('RadioDevice' and 'const RadioDevice') C ++错误:[对二进制表达式(&#39;std :: map的无效操作数 <int, std::function<void ()> ,std :: less <int> ...] - C++ error:[ invalid operands to binary expression ('std::map<int, std::function<void ()>, std::less<int>…] 错误:无效的二进制转换操作数(bitset&lt;8&gt; 和 int) - Error: Invalid operands to binary conversion(bitset<8> and int)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM