草庐IT

compound-operator

全部标签

c++ - 这个 operator() 语法有什么作用?

这段代码取自http://drdobbs.com/cpp/184403774:templateclassMinResult{L&lhs_;R&rhs_;public:operatorL&(){returnlhs_上面的代码试图在箭头指向的线上做什么?我是C++的初学者,我知道我们可以通过定义operator()来覆盖/定义它。但是不应该这样定义吗L&operator(){returnlhs_我确信这是一些不同的语法,因为operator()应该是一个词。此外,您不能用不同的返回类型定义其中两个。 最佳答案 不,这是类型转换运算符。你

c++ - OpenCV 2.1:ostream operator<< cv::Mat 在哪里?

正在关注thisquestion,我试图将cv::Mat的内容打印到标准输出:#include#include#include#include#includeintmain(){cv::Matm=cv::Mat::ones(10,10,CV_32S);std::cout这会导致错误error:nomatchfor‘operator我在Ubuntu11.10上使用gcc4.6.1,并安装了opencv,不包括示例,遵循theseinstructions.我的问题是,运算符在2.1中可用吗?如果可用,我如何获得它? 最佳答案 更新到Op

c++ - 类没有成员 "operator<<"

我已通读Shouldoperator和OverloadingInsertionOperatorinC++,看起来像类似的问题,但没有解决我自己的问题。我的头文件:usingnamespacestd;classAnimal{private:friendostream&operator我的客户:usingnamespacestd;intAnimal::getnumber(){returnnumber;}ostream&Animal::operator实现很简单,但我收到错误:在cpp中-错误:类“Animal”类没有成员“operator我真的不明白,因为我已经将插入运算符声明为Anima

c++ - "no ' operator++(int) ' declared for postfix '++ ' [-fpermissive]"枚举

这个问题在这里已经有了答案:HowcanIiterateoveranenum?(28个答案)Whycan'tIincrementavariableofanenumeratedtype?(10个答案)关闭9年前。我有枚举enumProgramID{A=0,B=1,C=2,MIN_PROGRAM_ID=A,MAX_PROGRAM_ID=C,}CurrentProgram;现在,我正尝试像这样递增CurrentProgram:CurrentProgram++,但编译器提示:没有为后缀'+声明'operator++(int)'+'[-fpermissive]。我认为有这样一个运算符可以增加“枚

c++ - 如何在 operator += 中调用 operator +

operator+=这样定义对吗?!voidoperator+=(constBigNumber&other){*this=(*this)+other;}在这样的类中:classBigNumber{public://....BigNumberoperator+(constBigNumber&other){returnsum(other);}//....} 最佳答案 是的。但正确的方法是根据operator+=来实现operator+:structfoo{intvalue;foo&operator+=(constfoo&other){v

c++ - ‘operator ==’ 中的 ‘a == b’ 不匹配

#includeusingnamespacestd;classfamily{private:doubleweight;doubleheight;public:family(doublex,doubley);~family();doublegetWeight();doublegetHeight();doublesetWeight();doublesetHeight();booloperator==(constfamily&,constfamily&);};boolfamily::operator==(constfamily&a,constfamily&b){return(a.getWei

c++ - 避免使用 "child"延迟 `operator<<` 对象构造

假设我有一个存储std::vector的容器对象多态child。structChild{Child(Parent&mParent){/*...*/}virtual~Child(){}};classParent{private:std::vector>children;templateauto&mkChild(TArgs&&...mArgs){//`static_assert`that`T`isderivedfrom`Child`...children.emplace_back(std::make_unique(std::forward(mArgs)...));return*childr

c++ - operator-> 重复直到它返回一个非类类型的值

根据13.3.1.2/8,或更好footnote-129(强调我的):[...]Theprocessrepeatsuntilanoperator->functionreturnsavalueofnon-classtype.我以为我知道operator->是如何工作的(让我说,它是基于返回类型的递归方式),但我发现我完全不知道关于它实际上是如何工作的(我的意思是,它的返回类型)。当我找到它时,我想知道是否真的可以为通用结构S定义和使用类似doubleoperator->()的东西,因为我已经从来没有这样使用过这样的运算符。例如,请考虑以下代码:structS{constexprdoubl

c++ - 类型没有可行的重载 operator[]

我定义了一个类型:typedefunordered_map>Graph;我有一把key:stringv="A12";当我尝试使用key访问列表时:for(autow=g[v].begin();w!=g[v].end();w++){...}g是Graph类型,我得到错误:Noviableoverloadedoperator[]fortype'constGraph'我该如何解决这个问题? 最佳答案 问题是g是一个constgraph&。索引运算符[]可能需要为map创建一个新元素,从而改变它。这就是为什么你不能在constgraph&上

c++ - operator<< 无法输出 std::endl -- 修复?

下面的代码在应该只输出std::endl时出错了:#include#includestructMyStream{std::ostream*out_;MyStream(std::ostream*out):out_(out){}std::ostream&operatorstructFoo{OutputStream*out_;Foo(OutputStream*out):out_(out){}voidtest(){(*out_)foo(&out);foo.test();returnEXIT_SUCCESS;}错误是:stream1.cpp:19:error:nomatchfor'operato