草庐IT

operator-keyword

全部标签

C++ 错误 : operator []: 2 overloads have similar conversions

templateclassv3{private:T_a[3];public:T&operator[](unsignedinti){return_a[i];}constT&operator[](unsignedinti)const{return_a[i];}operatorT*(){return_a;}operatorconstT*()const{return_a;}v3(){_a[0]=0;//works_a[1]=0;_a[2]=0;}v3(constv3&v){_a[0]=v[0];//Error1errorC2666:'v3::operator[]':2overloadshave

c++ - 如何直接调用 "operator->()"?

由于某些奇怪的原因,我需要直接调用operator->()方法。例如:classA{public:voidfoo(){printf("Foo");}};classARef{public:A*operator->(){returna;}protected:A*a;};如果我有一个ARef对象,我可以这样调用foo():aref->foo();但是,我想获取指向protected成员“a”的指针。我该怎么做? 最佳答案 aref.operator->();//ReturnsA*请注意,此语法也适用于所有其他运算符://AssumingA

C++:如何阻止 map 的 operator[] 插入虚假值?

我的代码做了以下事情:使用operator[]从map中检索值。检查返回值,如果NULL使用insert在map中插入一个新元素。神奇的是,一个值为0的元素出现在map中。经过几个小时的调试,我发现了以下内容:map的operator[]insertsanewelementifthekeyisnotfound而插入doesnotchangethevalueifthekeyexists.即使映射值类型的默认构造函数不存在,代码也会编译并且operator[]插入0。有没有什么办法(例如,从现在开始我可以遵循的一些编码约定)我可以防止这对我造成伤害? 最佳答案

c++ - std::vector<A> error C2582: 'operator =' 函数在

我使用简单的vectorpush_back到类型A的对象并收到此错误,这是我的代码:classA{public:A(inta,intb,intc);};#include"A.h"................std::vector*vec_objects=newstd::vector();while(....somecondition...){Aa(1,2,3)vec_objects->push_back(a);}出现这个错误:c:\programfiles\microsoftvisualstudio9.0\vc\include\xutility(3159):errorC2582:'

c++ - 什么是 do(keyword) c++?

我从来不知道c++中有关键字do!这是什么? 最佳答案 这是一个循环:do{...}while(someCondition); 关于c++-什么是do(keyword)c++?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4531873/

c++ - 与运算符重载不匹配运算符+错误

我正在学习gameinsitute的C++编程类(class),其中有一个运算符重载的示例,我不断得到一个main.cpp|20|error:nomatchfor‘operator+’in‘v+w’我不知道问题出在哪里。主要.cpp//main.cpp#include"Vector3.h"#includeusingnamespacestd;intmain(){floatcoords[3]={1.0f,2.0f,3.0f};Vector3u;Vector3v(coords);Vector3w(-5.0f,2.0f,0.0f);coutVector3.h#ifndefVECTOR3_H#d

c++ - 为什么 uBLAS 没有 `operator*(matrix, vector)` ?

在doc,他们说Wedecidedtousenooperatoroverloadingfor...他们为这些提供了prod。但为什么?有什么好的理由吗?我喜欢做matrix*vector(和大多数其他语言一样)。我想了解为什么他们没有重载此运算符以了解为什么自己做可能是个坏主意。或者,如果我自己重载,它们不会有任何缺点吗? 最佳答案 可能是因为op*在其他语言中,例如使用Python中的Numpy,将始终是元素明智的。如果一个元素是矩阵而另一个元素是vector,它将尝试广播缺失维度中的所有元素。

c++ - std::map 是否要求比较器的 operator() 为常量?

在OSX10.8上使用libc++时,以下代码无法使用XCode4.5的clang++进行编译:#include#includeclassFoo{public:explicitFoo(intval_):val(val_){}intval;};structFooComparator{booloperator()(constFoo&left,constFoo&right){returnleft.valm;Foof(4);m[f]=std::string("four");return0;}错误:broken.cpp:11:8:note:candidatefunctionnotviable:'

c++ - 为什么不使用强制转换语法调用 "operator void"?

在玩thisanswer时通过userGMan我制作了以下代码片段(使用VisualC++9编译):classClass{public:operatorvoid(){}};Classobject;static_cast(object);(void)object;object.operatorvoid();通过调试器后,我发现转换为void不会调用Class::operatorvoid(),只有第三次调用(显式调用运算符)实际上调用了运算符,这两个转换什么都不做。为什么operatorvoid没有用强制转换语法调用? 最佳答案 在§1

c++ - "warning: operation of ... may be undefined"用于三元运算——不是 if/else block

这个问题在这里已经有了答案:Undefinedbehaviorandsequencepoints(5个答案)关闭6年前。这是我的代码:intmain(){staticinttest=0;constintanotherInt=1;test=anotherInt>test?test++:0;if(anotherInt>test)test++;elsetest=0;return0;}这是我构建它时产生的警告:../main.cpp:15:40:warning:operationon‘test’maybeundefined[-Wsequence-point]test=anotherInt>te