二进制表达式的操作数无效("int_node"和常量"int_node")
invalid operands to binary expression ('int_node' and const 'int_node')
我是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);
}
我的编译器说
"1>main.cpp(28):错误C2676:binary"!=":"t_node"未定义此运算符或到类型的转换预定义操作员可接受"
这是真的。
我们可以根据==
定义!=
,例如您丢失的int_node
bool operator == (const int_node &i) const {return val == i.val;}
bool operator != (const int_node &i) const {return !(*this==i);}
您需要定义运算符-他们是否也应该检查节点?
顺便说一句,你打算无论如何都归还first
吗?
while (first != last && *first != value)
{
++first;
return first;
// ^^^^^
// |||||
}
我看到了两件事:
struct int_node
在它自己和const int_node &
实例之间没有定义任何比较运算符,它回答了您的问题- 在函数
Iterator find(Iterator first, Iterator last, const T& value)
中,while
语句中有一个return
语句,因此while
是无用的,或者return
应该放在它之外
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- node-gyp 在 macOS 上未正确链接库
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 无法从'int'中推断出'Node<T> *'的模板参数
- 链表(从 'Node<int>*' 到 'int' 的转换无效 [-fallowive]|)
- 将链表的头部传递给函数时。为什么我们需要通过引用来传递它,例如在 push(node* &head, int key)
- '=' : 无法从'CircularDoubleDirectedList<int>::Node *'转换为'Node *'
- C++模板方法:为什么 Node<int> 没问题,但 Node 不行<float>?
- 无法编译C++代码:从'Node*'到'int'的转换无效
- Native Node.JS模块-解析参数中的int[]
- C2440 '=' 无法从 'int' 转换为 'BST:<int>:Node*
- 成员引用基类型 'Node<int> *' 不是结构或联合
- 映射和节点,错误智能感知:不存在从"const std::pair<const int, double>"到"Node"的合适用户定义转换