草庐IT

binary-arithmetic-operations

全部标签

C++ 警告 : suggest parentheses around arithmetic in operand of |

我有这样的代码A=B|C|D|E;抛出警告“建议在|的操作数中使用括号”期望表达式需要运算符的高优先级括号,尝试了以下方式:A=(B|C)|(D|E);还有一个:A=(((B|C)|D)|E);仍然存在相同的警告。请帮我解决这个问题。谢谢,素加他B、C、D是枚举,E是整数。 最佳答案 你的表达式中有一些算术运算符不是真正简单的B,或者不是真正简单的C,等等。编译器建议你用括号括起任何表达式,以便读者看到你写的是你的意思.如果你不加括号,每个人都必须准确记住优先级是什么,他们必须弄清楚你是否记得你写的时候。试试这个:(B)|(C)|(

c++ - “no match for ' operator< '” 尝试插入到 std::set 时

我正在使用gcc4.3.3尝试编译以下代码:structtestStruct{intx;inty;booloperatorsetti;setti.insert(testStruct(10,10));return0;}我得到这个错误:/usr/include/c++/4.4/bits/STL_function.h|230|错误:‘__x我怀疑我没有像应该做的那样重载运算符,但我无法查明确切的问题。我在这里做错了什么? 最佳答案 运算符必须是const并且取一个const引用:booloperator

c++ - 不明确的 string::operator= 调用隐式转换为 int 和 string 的类型

给定以下程序:#include#includeusingnamespacestd;structGenericType{operatorstring(){return"HelloWorld";}operatorint(){return111;}operatordouble(){return123.4;}};intmain(){inti=GenericType();strings=GenericType();doubled=GenericType();cout它在VisualStudio11上编译,但不是clang或gcc。它有问题,因为它想从GenericType隐式转换为int为cha

c++ - 如何为聚合结构实现 C++ (in)equality operators?

有时我有这样的结构——structaggregate1{std::stringname;std::vectoroptions;size_tfoobar;//...};--其中(不)平等被简单地定义为所有成员的(不)平等:lhs_name==rhs_name&&lhs_options==rhs_options&&lhs_foobar==rhs_foobar.实现它的“最佳”方法是什么?(最好的是:(运行时-)效率、可维护性、可读性)operator==在operator!=方面operator!=在operator==方面==的单独实现和!=作为成员(member)还是作为免费功能?请注

c++ - '&' : illegal operation on bound member function expression

这个问题在这里已经有了答案:Printaddressofvirtualmemberfunction(5个答案)关闭7年前。当我尝试从具有主要功能的单个cpp文件时,这有效,sprintf(smem_options,"#transcode{vcodec=RV24}:smem{""video-prerender-callback=%lld,""no-time-sync},",(longlongint)(intptr_t)(void*)&cbVideoPrerender);如何在类中将函数参数传递给sprintf?sprintf(smem_options,"#transcode{vcodec

c++ Overload operator bool() 使用 operator+ 给出模糊的重载错误

我正在编译MegaInt类的一些C++代码,它是一个允许对大数进行算术运算的正十进制类型类。我想重载operatorbool以允许这样的代码:MegaIntm(45646578676547676);if(m)cout这是我做的:标题:classMegaInt{public:...operatorbool()const;};constMegaIntoperator+(constMegaInt&left,constMegaInt&right);constMegaIntoperator*(constMegaInt&left,constMegaInt&right);实现:MegaInt::op

c++ - 简单的 C++ : How to overload the multiplication operator so that float*myClass and myClass*float works

classMyClass;intmain(){floata=5;MyClassc1;MyClassc2=a*c1;MyClassc3=c1*a;}如何重载乘法运算符以便a*c1和c1*a都能工作? 最佳答案 像这样:MyClassoperator*(floatx,constMyClass&y){//...}MyClassoperator*(constMyClass&y,floatx){//...}第二个也可以是成员函数:classMyClass{//...MyClassoperator*(floatx);};前2个选项作为类范围之外

c++ - 重载 operator== 提示 'must take exactly one argument'

我试图重载operator==,但编译器抛出以下错误:‘boolRationalnumber::operator==(Rationalnumber,Rationalnumber)’musttakeexactlyoneargument我的一小段代码如下:boolRationalnumber::operator==(Rationalnumberl,Rationalnumberr){returnl.numerator()*r.denominator()==l.denominator()*r.numerator();}声明:booloperator==(Rationalnumberl,Rati

c++ - 使用 std::map 时,我应该为键类型重载 operator== 吗?

std::map不应该有重复的键,所以当我有一个自定义类型时它怎么知道我有一个重复的键,我需要做重载运算符==吗?或者它会被隐式创建?根据文档,我只需要运算符考虑这个例子:classMyType{public:MyType(intnewId){id=newint;*id=newId;};~MyType{deleteid;}private:int*id;};intmain(){std::mapmyMap;std::pair::iterator,bool>ret;ret=myMap.insert(std::pair(myType(2),100));if(!ret.second){//now

c++ - 为什么只有 random-access-iterator 在 C++ 中实现 operator+?

我想为STLlist迭代器获取farnext值,但它没有实现operator+,不过vector有它。为什么以及如何获得我想要的值(value)?我想如果我多次调用operator++就可以做到这一点,但这不是有点脏吗?我想做的是:listl;...omitted...list::iteratoritr=l.begin()+3;//but,listiteratordoesnothave//operator+什么是我想要的最佳解决方案? 最佳答案 您想使用std::advance:list::iteratoritr=l.begin()