2

私は C++ の初心者で、私の周りには多くの質問があります。!=の演算子を定義int_nodeしましたが、このコードをコンパイルするとエラーが表示されます:

バイナリ式 ('int_node' および const 'int_node') に対する無効なオペランド

私が使用している IDE は xcode 4.6 です。

以下は私のすべてのコードです

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

2 に答える 2

1

私はそこに2つのものを見ます:

  • struct int_nodeそれ自体とインスタンスの間の比較演算子を定義せず、const int_node &それはあなたの質問に答えます;
  • 関数Iterator find(Iterator first, Iterator last, const T& value)では、returnステートメント内にwhileステートメントがあるため、whileは役に立たないか、return外側に配置する必要があります。
于 2013-07-18T16:44:13.430 に答える