二进制表达式(';int#u node';和const';int#u node';)的操作数无效 我是C++初学者,我周围有很多问题。我已经定义了=int_节点的运算符,但编译此代码时,显示错误:
二进制表达式('int_node'和常量'int_node'的操作数无效) 我使用的IDE是xcode 4.6 下面是我的全部代码二进制表达式(';int#u node';和const';int#u node';)的操作数无效 我是C++初学者,我周围有很多问题。我已经定义了=int_节点的运算符,但编译此代码时,显示错误:,c++,C++,二进制表达式('int_node'和常量'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){} No
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);
}
typedef结构内部节点{
int-val;
结构int_节点*下一步;
}国际;
模板
结构节点{
节点*ptr;
node_wrap(node*p=0):ptr(p){}
节点和运算符*()常量{return*ptr;}
节点*运算符->()常量{return ptr;}
node_wrap&operator++(){ptr=ptr->next;返回*this;}
node_wrap操作符++(int){node_wrap tmp=*this;++*this;return tmp;}
布尔运算符==(const node_wrap&i)const{return ptr==i.ptr;}
布尔运算符!=(const node_wrap&i)const{return ptr!=i.ptr;}
} ;
模板
迭代器查找(迭代器优先、迭代器最后、常量T和值)
{
while(first!=last&&*first!=value)//二进制表达式('int\u node'和const'int\u node'的操作数无效)
{
++第一,;
先返回;
}
}
int main(int argc,const char*argv[]
{
struct int_node*list_head=nullptr;
struct int_node*list_foot=nullptr;
结构内部节点valf;
valf.val=0;
valf.next=nullptr;
查找(node_wrap(list_head)、node_wrap(list_foot)、valf);
返回(0);
}
我的编译器说
“1>main.cpp(28):错误C2676:
二进制“!=”:
'int_node'未定义此运算符或类型转换
预定义操作员可接受”
这是真的
我们可以定义=代码>在==
方面,例如,对于缺少的int\u节点
bool operator == (const int_node &i) const {return val == i.val;}
bool operator != (const int_node &i) const {return !(*this==i);}
您需要定义操作符-它们是否也要检查节点
顺便问一下,你打算先返回吗
while (first != last && *first != value)
{
++first;
return first;
// ^^^^^
// |||||
}
我在那里看到两件事:
struct int_节点
不会在自身和const int_节点&
实例之间定义任何比较运算符,这会回答您的问题李>
- 在函数
Iterator find(Iterator first,Iterator last,const T&value)
中,while
语句中有一个return
语句,因此while
是无用的,或者return
应该放在它的外部
A)请缩进您的代码B)您说您定义了运算符=代码>用于int_node,但我只在node_wrapt中看到它谢谢,是的,我没有定义运算符!=对于int_节点。我是一个初学者,所以当我问这个问题时,我不知道操作符重载。事实是,在这种情况下,它是必不可少的。编译器不知道如何处理int\u节点
和const int\u节点
之间的比较,只需定义bool操作符之类的东西=(const int_node&i){return val!=i.val;}
就是这个问题的解决方案:它需要另一个int_node
引用,它是const
,因为您不需要修改它,并且返回一个布尔值(我假设真正的比较是两个int_node::val
),我将您提供的代码添加到我的项目中,Successed。在你的帮助下,我知道了错误。比你强。我是中国人,我的英语很好garbage@BestWater祝你好运对不起,我也想照你说的去做,但是我的英语很差,而且是第一次使用这个网站。所以我不知道怎么给你打勾。请告诉我。